Last active
August 29, 2015 14:02
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void send(String senderMail, String senderName, List<String> receiverList, String subject, String content) | |
{ | |
LOGGER.info("send"); | |
Properties props = System.getProperties(); | |
props.put("mail.transport.protocol", "smtp"); | |
props.put("mail.smtp.host", "email-smtp.us-west-2.amazonaws.com"); // 到https://console.aws.amazon.com/ses/下的SMTP Setting可以你的主機位址 | |
props.put("mail.smtp.port", 25); | |
props.put("mail.smtp.auth", "true"); | |
props.put("mail.smtp.starttls.enable", "true"); | |
props.put("mail.smtp.starttls.required", "true"); | |
Session mailSession = Session.getInstance(props, | |
new javax.mail.Authenticator() | |
{ | |
protected PasswordAuthentication getPasswordAuthentication() | |
{ | |
return new PasswordAuthentication( | |
"填入SMTP Credentials的帳號", "填入SMTP Credentials的密碼"); | |
} | |
}); | |
try | |
{ | |
Message msg = new MimeMessage(mailSession); | |
msg.setFrom(new InternetAddress(senderMail, senderName, "UTF-8")); | |
for (int i = 0; i < receiverList.size(); i++) | |
{ | |
String receiver = receiverList.get(i); | |
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(receiver)); | |
} | |
msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", "B") ); | |
msg.setContent(content, "text/html; charset=UTF-8"); | |
Transport.send(msg); | |
} | |
catch (AddressException e) | |
{ | |
LOGGER.error(e.getLocalizedMessage()); | |
e.printStackTrace(); | |
} | |
catch (MessagingException e) | |
{ | |
LOGGER.error(e.getLocalizedMessage()); | |
e.printStackTrace(); | |
} | |
catch (UnsupportedEncodingException e) | |
{ | |
LOGGER.error(e.getLocalizedMessage()); | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment