報錯代碼
javax.mail.MessagingException: Could not convert socket to TLS;
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
首先有道翻譯后看着好像是證書問題。然后百度了下。看到很多人遇到這些問題,就一個個試一下
原先獲取 smtp 的寫法
Properties props = new Properties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
后來試下加上了 代碼(host是郵箱服務器地址),可以發送了。網上說的大概意思就是設置服務是可以信任的,不需要安全證書
props.put("mail.smtp.ssl.trust", host);
如果還不可以可能就是調用ssl加密 ,網上搜下怎么發送ssl加密的發送郵件方法
或者試試創建session的其他方法
Session session = Session.getInstance(props, new javax.mail.Authenticator()
{ protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password); } }
);
