网上找了一大圈,大部分都是让安装证书,或者忽略ssl加密
都是比较繁琐的
后来找到一种方式,3行代码就可以解决这个问题
oracle官网解释:
https://www.oracle.com/java/technologies/javamail-sslnotes.html
最方便的解决方式
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
// or
// sf.setTrustedHosts(new String[] { "my-server" });
props.put("mail.smtp.ssl.enable", "true");
// also use following for additional safety
//props.put("mail.smtp.ssl.checkserveridentity", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
完整代码:
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.smtp.host", stmp.**.com );
// 使用ssl端口为465
props.setProperty("mail.smtp.port", 465);
//*********开启加密协议*************//
props.setProperty("mail.smtp.ssl.enable", "true");
// 简化证书认证
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.socketFactory", sf);
参考链接:https://blog.csdn.net/yuli001123/article/details/84801721