最初是用qq邮件测试的,没问题。现场的邮箱服务遇到一些问题,记录一下。
问题一
STARTTLS is required to send mail
添加配置后解决
mailProps.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
有blog说添加如下配置可以解决,然并卵
mailProps.put("mail.smtp.ssl.trust", mailServer.getSmtpHost());
报错中可以看出是证书的问题,
详细解释
打开 https://javaee.github.io/javamail/FAQ#installcert ,搜InstallCert;
或 直接打 https://javaee.github.io/javamail/InstallCert
解决过程
1 jar包下载:
https://download.csdn.net/download/sinaihalo/11162714
2 jar包执行指令:
java -jar C:\Users\dell\Desktop\prt\InstallCert.jar hostip
3 源码
见 https://javaee.github.io/javamail/InstallCert.java
4 结果
java -jar /home/ultrapower/OutManager/InstallCert.jar 10.236.3.246
Loading KeyStore /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-7.b13.el7.x86_64/jre/lib/security/cacerts...
Opening connection to 10.236.3.246:443...
Starting SSL handshake...
Server sent 2 certificate(s):
...
Enter certificate to add to trusted keystore or 'q' to quit: [1]
1 //输1,回车
...
Added certificate to keystore 'jssecacerts' using alias '10.236.3.246-1'
5 复制重启
证书生成在jar包路径,复制证书文件到Java安装路径\jre\lib\security,重启服务后能收邮件了。