Как я прокомментировал с OP, может быть проблема с вашим подходом к соединению SSL / TLS. Попробуйте этот пример кода с вашими собственными настройками, я успешно использовал его в самых разных средах. Или, может быть, вы можете просто выбрать важную часть кода из примера:
private void sendMail()
throws MessagingException, AddressException, IOException {
String smtpHost = "";
Integer smtpPort = 465;
final String from = "me@me.com";
final String password = "";
String to = "theOtherGuy@he.com";
String cc = "";
String bcc = "";
String subject = "";
String bodyText = "";
List<File> files = null; // Set attachment files if needed
boolean sslEnable = true;
boolean startTlsEnable = false;
String user = "";
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.port", smtpPort);
if (user != null && !user.equals("")) {
properties.put("mail.smtp.user", user);
properties.put("mail.smtps.user", user);
} else {
properties.put("mail.smtp.user", from);
properties.put("mail.smtps.user", from);
}
properties.put("mail.smtp.sendpartial", "true");
properties.put("mail.smtps.host", smtpHost);
properties.put("mail.smtps.port", smtpPort);
properties.put("mail.smtps.sendpartial", "true");
properties.put("mail.smtp.timeout", 30000);
properties.put("mail.smtp.connectiontimeout", 60000);
properties.put("mail.smtp.writetimeout", 15000);
if (startTlsEnable) {
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtps.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust", smtpHost);
properties.put("mail.smtps.ssl.trust", smtpHost);
properties.put("mail.protocol.ssl.trust", smtpHost);
}
if (sslEnable) {
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.trust", smtpHost);
properties.put("mail.smtps.ssl.enable", "true");
properties.put("mail.smtps.ssl.trust", smtpHost);
properties.put("mail.protocol.ssl.trust", smtpHost);
}
Session session;
if (password != null && password.length() > 0) {
properties.put("mail.smtp.auth", "true");
final String userName = user;
if (user != null && !user.equals("")) {
session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName,
password);
}
});
} else {
session = Session.getInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,
password);
}
});
}
} else {
session = Session.getInstance(properties);
}
MimeMessage mailMessage = new MimeMessage(session);
try {
mailMessage.setFrom(new InternetAddress(from));
} catch (AddressException e) {
throw new AddressException("Invalid FROM address: " + e.getMessage());
}
mailMessage.setRecipients(Message.RecipientType.TO, to);
mailMessage.setRecipients(Message.RecipientType.CC, cc);
mailMessage.setRecipients(Message.RecipientType.BCC, bcc);
mailMessage.setSubject(subject);
mailMessage.setSentDate(new Date());
// Set the email message text.
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setContent(bodyText, "text/html; charset=utf-8");
// messagePart.setText(bodyText);
Multipart multipart = new MimeMultipart("mixed");
multipart.addBodyPart(messagePart);
try {
if (files != null) {
for (File file : files) {
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.attachFile(file);
multipart.addBodyPart(attachmentPart);
}
}
} catch (MessagingException e) {
throw new MessagingException(
"Error attaching files, MessagingException: "
+ e.getMessage());
} catch (IOException e) {
throw new IOException("Error attaching files: " + e.getMessage());
}
mailMessage.setContent(multipart);
if (password == null || password.length() < 1) {
Transport.send(mailMessage, mailMessage.getAllRecipients());
} else if (user != null && !user.equals("")) {
Transport.send(mailMessage, mailMessage.getAllRecipients(), user,
password);
} else {
Transport.send(mailMessage, mailMessage.getAllRecipients(), from,
password);
}
}