javax.mail.MessagingException: Could not connect to SMTP host: xxxxx, port: 465;
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
Caused by: 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
代码如下
Properties properties = System.getProperties();
properties.put("mail.transport.protocol","smtp");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback","false");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.socketFactory.port","465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory",new MailSSLSocketFactory());
properties.put("mail.smtp.ssl.checkserveridentity", true);
Session session = Session.getInstance(properties);
session.setDebug(true);
这里报错时
MailSSLSocketFactory引用的为
com.sun.mail.util.MailSSLSocketFactory
解决问题方案替换MailSSLSocketFactory为下面的类,
import lombok.extern.slf4j.Slf4j;
import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
@Slf4j
public class MailSSLSocketFactory extends SSLSocketFactory {
private SSLSocketFactory factory;
public MailSSLSocketFactory() {
try {
SSLContext sslcontext = SSLContext.getInstance("TLSv1.2");
sslcontext.init(null, new TrustManager[] { new MailTrustManager() }, null);
factory = sslcontext.getSocketFactory();
} catch (Exception ex) {
// ignore
log.info(ex.getMessage(),ex);
}
}
public static SocketFactory getDefault() {
return new MailSSLSocketFactory();
}
@Override
public Socket createSocket() throws IOException {
return factory.createSocket();
}
@Override
public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
return factory.createSocket(socket, s, i, flag);
}
@Override
public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
return factory.createSocket(inaddr, i, inaddr1, j);
}
@Override
public Socket createSocket(InetAddress inaddr, int i) throws IOException {
return factory.createSocket(inaddr, i);
}
@Override
public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
return factory.createSocket(s, i, inaddr, j);
}
@Override
public Socket createSocket(String s, int i) throws IOException {
return factory.createSocket(s, i);
}
@Override
public String[] getDefaultCipherSuites() {
return factory.getDefaultCipherSuites();
}
@Override
public String[] getSupportedCipherSuites() {
return factory.getSupportedCipherSuites();
}
}