public static void sendMail0(String smtpServer, String smtpPort, final String smtpLogin, final String smtpPassword, String emailRecipients, String emailSubject, MessageSetter messageSetter) {
try {
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", smtpServer);
properties.setProperty("mail.smtp.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.port", smtpPort);
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.smtp.socketFactory.fallback", "false");
properties.setProperty("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpLogin, smtpPassword);
}
});
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(smtpLogin));
for (String recipient : StringUtils.split(emailRecipients, ",")) {
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
}
message.setSubject(emailSubject, "UTF-8");
messageSetter.set(message);
Transport.send(message);
log.debug("Письмо успешно отправлено по адресам: " + emailRecipients);
} catch (MessagingException e) {
log.debug("MessagingException", e);
}
}
new MessageSetter() {
@Override
public void set(MimeMessage mimeMessage) throws MessagingException {
mimeMessage.setContent(emailHtmlText, "text/html; charset=UTF-8");
}
}
У меня работает этот код.