我正在尝试通过Gmail imap协议连接到Gmail帐户
这是我正在调用的方法:
public EmailHelper() throws MessagingException, GeneralSecurityException {
String username = "[email protected]";
String password = "emailpassword";
String server = "imap.gmail.com";
Properties props = System.getProperties();
props.setProperty("mail.imap.ssl.enable", "true");
props.setProperty("mail.store.protocol", "imaps");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
props.put("mail.imaps.ssl.checkserveridentity", "false");
props.put("mail.imaps.ssl.trust", "*");
props.put("mail.imaps.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect(server, username, password);}
这是我得到的例外:
Exception in thread "main" javax.mail.MessagingException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target;
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
我已经阅读并尝试了其他问题中发布的解决方案,但它们对我不起作用。我想我缺少了一些东西
非常感谢您的协助。谢谢。
如javamail docs中IMAP提供程序的文档所述,mail.imap属性没有“ s”变体-没有“ mail.imaps.ssl”,只有“ mail.imap.ssl”
将代码更改为
props.put("mail.imap.ssl.checkserveridentity", "false");
props.put("mail.imap.ssl.trust", "*");
props.put("mail.imap.ssl.socketFactory", sf);
应产生结果。实际上,仅中线应该已经做到了。