@Test
public void testGMail() throws Exception {
Properties props = System.getProperties();
String[] tos = {"XXX,YYY,ZZZ"};
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
user = "Gmail email address";
pass = "XXXX";
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, pass);
}
});
javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session);
message.setFrom(new javax.mail.internet.InternetAddress(from));
for(String to : tos) {
message.addRecipient(Message.RecipientType.TO, new javax.mail.internet.InternetAddress(to));
}
message.setSubject("Test gmail");
message.setContent("Test gmail content", "text/html");
Transport.send(message);
System.out.println("Done.");
}
No comments:
Post a Comment