我想通过访问POP3服务器来下载邮件SSL是在本地环境中配置的,但是我们失败了。
连接时需要采取哪些步骤?
异常
javax.mail.MessagingException: Connect failed;
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
示例代码
Properties props = new Properties();
final String HOST = "host";
final String PROTOCOL = "pop3";
final String PORT = "995";
String username = "user";
String pass = "password";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", PORT);
props.setProperty("mail.pop3.socketFactory.port", PORT);
URLName urln = new URLName(PROTOCOL, HOST, Integer.parseInt(PORT), null, username, pass);
Session session = Session.getInstance(props, null);
Store store = session.getStore(urln);
store.connect();
答案 0 :(得分:1)
设置属性mail.pop3.ssl.trust
props.setProperty("mail.pop3.ssl.trust", "*"); // Trust all Servers
您还应该只能将其设置为您的特定服务器,然后使用*进行测试
props.setProperty("mail.pop3.ssl.trust", HOST);