【发布时间】:2016-03-09 15:19:14
【问题描述】:
以下代码作为 Java 应用程序 eclipse 运行,但是当它部署在 SAP PO 服务器中时,会引发以下异常:
注意:独立的 java 文件在我的笔记本电脑(Windows)上运行,因为服务器不是 Linux
javax.mail.MessagingException:无法将套接字转换为 TLS; 嵌套异常是:java.net.SocketException: java.security.NoSuchAlgorithmException:构造错误 实现(算法:默认,提供者:SunJSSE,类: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)
public static void main(String[] args) {
PropertiesSerDto dto = new PropertiesSerDto();
dto.setEmailAuth("true");
dto.setEmailStarttlsEnable("true");
dto.setEmailHost("outlook.office365.com");
dto.setEmailPort("587");
dto.setEmailPassword("1234");
dto.setEmailFrom("test@mail.com");
dto.setEmailUser("test@mail.com");
String to="to@gmail.com";
String subject="Mail Testing";
String message = "Hello";
try {
sendHtmlEmail(dto,to,subject,message);
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void sendHtmlEmail(PropertiesSerDto dto, String to, String subject, String message) throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", dto.getEmailHost());
properties.put("mail.smtp.port", dto.getEmailPort());
properties.put("mail.smtp.auth", dto.getEmailAuth());
properties.put("mail.smtp.starttls.enable", dto.getEmailStarttlsEnable());
final String user = dto.getEmailUser();
final String password = dto.getEmailPassword();
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message messageContent = new MimeMessage(session);
messageContent.setFrom(new InternetAddress(dto.getEmailFrom()));
InternetAddress[] iAdressArray = InternetAddress.parse(to);
messageContent.setRecipients(Message.RecipientType.TO, iAdressArray);
messageContent.setSubject(subject);
messageContent.setSentDate(new Date());
// set plain text message
messageContent.setContent(message, "text/html");
// sends the e-mail
Transport.send(messageContent);
}
【问题讨论】:
-
我不知道“SAP PO 服务器”是什么,但看起来它改变了 JDK 的安全配置,以防止它使用默认的 SSLSocketFactory 创建 SSL 套接字。比较工作机器和非工作机器之间的 jre/lib/security/java.security 文件。
-
您好,此代码是 java 版本 1.6.0_18 可以工作,但 1.6.0_81 这个版本不工作
-
较新版本的 JDK 加强了与 SSL/TLS 相关的安全配置。我不知道细节,但我没想到你会得到错误。通过打开SSLNOTES.txt 中所述的 JSSE 调试选项,您可能会获得更多有助于诊断问题的信息。
-
正如您提到的 yoi deploy on SAP PO 它可能在 SAP JVM 上运行,它与该 JVM 高度相关(这是 SAP 自己实现/重新打包的标准 JVM)
标签: java email outlook jakarta-mail office365