报错如下
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=