-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaMailService.java
More file actions
74 lines (60 loc) · 1.89 KB
/
JavaMailService.java
File metadata and controls
74 lines (60 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Date;
import java.util.Properties;
import java.util.Set;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Component
public class MailService {
private Properties props;
private Session session;
private String senderHost;
private String senderServer;
private String senderAddress;
private String senderPassword = "";
private String recipientAddress;
private String emailContent;
public MailService(){
senderHost = "localhost";
senderServer = "yular";
senderAddress = "yular@gmail.com";
props = System.getProperties();
props.setProperty("mail.smtp.host", senderHost);
session = Session.getDefaultInstance(props);
emailSubject = "";
emailContent = "";
recipientAddress = " ... ";
}
public void sendMailNotification()
{
try
{
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderAddress));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(recipientAddress));
generateMessage();
msg.setSubject(emailSubject);
msg.setSentDate(new Date());
msg.setContent(emailContent, "text/html; charset=utf-8"); // The type of email content can be changed.
Transport.send(msg);
}
catch (AddressException e)
{
System.out.println(" Send Fail, Address Exception : " + e);
}
catch (MessagingException e)
{
System.out.println(" Send Fail, Message Exception : " + e);
}
}
//Set the message content there
private void generateMessage(){
emailSubject = " ... ";
emailContent = " ........ ";
}
}