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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* Simple SMTP mail class built using the example from: <a
* href="http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example">mkyong.com</a>
*
* <p>If you get {@link java.net.UnknownHostException}: smtp.gmail.com , try
* ping smtp.gmail.com and make sure you got a response (able to access). Often
* times, your connection may block by your firewall or proxy behind.</p>
*/
public class MailSender {
private String userName;
private String password;
private Properties props;
private final static String CONTENT_CHARSET = "text/html; charset=utf-8";
public MailSender(Properties props) {
this.userName = (String) props.get("userName");
this.password = (String) props.get("password");
this.props = props;
}
public void sendMail(String subject, String bodyText) throws MessagingException {
sendMail(subject, bodyText, false);
}
public void sendMail(String subject, String bodyText, boolean isHtmlBody) throws MessagingException {
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message message = new MimeMessage(session);
// set from (sender) address
message.setFrom(new InternetAddress((String) props.get("fromAddress")));
// set recipients addresses (may be more than one)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse((String) props.get("recipientsAddresses")));
// set message subject
message.setSubject(subject);
// if it is html change multipart information
if (isHtmlBody) {
message.setContent(bodyText, CONTENT_CHARSET);
} else {
message.setText(bodyText);
}
Transport.send(message);
System.out.println("Done");
}
public static void main(String[] args) throws MessagingException {
Properties proper = new Properties();
// put credentials
proper.put("userName", "user");
proper.put("password", "password");
// put server information
proper.put("mail.smtp.host", "smtp.gmail.com");
proper.put("mail.smtp.socketFactory.port", "465");
proper.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
proper.put("mail.smtp.auth", "true");
proper.put("mail.smtp.port", "465");
// if you do not want to use authentication
//proper.put("mail.smtp.host", "192.168.1.13");
//proper.put("mail.smtp.socketFactory.port", "25");
//proper.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//proper.put("mail.smtp.connectiontimeout",30000);
//proper.put("mail.smtp.auth", "false");
//proper.put("mail.smtp.port", "25");
// put sender information
proper.put("fromAddress", "[email protected]");
// put recipient information
proper.put("recipientsAddresses", "[email protected]");
new MailSender(proper).sendMail("Bob", "Some text body for bob");
}
}
|