Java Mail无法找到有效的证书路径

时间:2014-08-10 13:55:25

标签: java ssl gmail javamail

我正在尝试为Windows制作一个gmail客户端来学习Swing,我在使用JavaMail阅读IMAP电子邮件时遇到了问题。

我见过很多关于这个问题的线索,他们中的大多数人说我必须将证书路径添加到JRE。

我用openssl获得了证书并将其添加到密钥库但我一直收到同样的错误:

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

这是我的代码:

final String username = setting.getAddress();
final String password = setting.getPassword();
String receivingHost="imap.gmail.com";//for imap protocol

Properties props2=System.getProperties();
props2.setProperty("mail.store.protocol", "imaps");

Session session2=Session.getDefaultInstance(props2, null);
try {
    Store store=session2.getStore("imaps");
    store.connect(receivingHost,username, password);
    Folder folder=store.getFolder("INBOX");//get inbox
    folder.open(Folder.READ_ONLY);//open folder only to read
    Message message[]=folder.getMessages();
    for(int i=0;i<message.length;i++){
        System.out.println(message[i].getSubject());
    }

    folder.close(true);
    store.close();
} catch (Exception e) {
    System.out.println(e.toString());
}

0 个答案:

没有答案