報錯如下
java代碼中添加如下方法 props.put("mail.smtp.starttls.enable", "true"); 報錯變化為如下
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
缺少證書鏈的認證 需要將證書導入打jdk中(更換IBMJDK也解決了問題*)
1.獲取證書
openssl s_client -showcerts -starttls smtp -crlf -connect smtp.office365.com:587 < /dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > public.crt
2.導入證書到jdk 密碼是 changeit 或changit
keytool -import -alias outlook -keystore $JAVA_HOME/jre/lib/security/cacerts -file public.crt
keytool -list -keystore $JAVA_HOME/lib/security/cacerts
即可
*IBMJDK會報錯
javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
java.net.SocketException: java.lang.ClassNotFoundException: Cannot find the specified class com.ibm.websphere.ssl.protocol.SSLSocketFactory
解決方法
在<IBM_JAVA_HOME>/jre/lib/security/java.security文件尾添加以下內容:
ssl.SocketFactory.provider=
ssl.ServerSocketFactory.provider=