Skip to content

Instantly share code, notes, and snippets.

@tangblack
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tangblack/1c8ed79e8d80b44f9738 to your computer and use it in GitHub Desktop.
Save tangblack/1c8ed79e8d80b44f9738 to your computer and use it in GitHub Desktop.
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