尝试连接到gmail IMAP服务器时出现javax.mail.MessagingException

问题描述 投票:2回答:1

我正在尝试通过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

我已经阅读并尝试了其他问题中发布的解决方案,但它们对我不起作用。我想我缺少了一些东西

非常感谢您的协助。谢谢。

java email smtp javamail imap
1个回答
0
投票

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);

应产生结果。实际上,仅中线应该已经做到了。

© www.soinside.com 2019 - 2024. All rights reserved.