我正在尝试使用 Javamail 连接到我的 gmail 帐户。它不连接并引发错误。这是我的代码 -
public class MailConnection {
Folder inbox;
public MailConnection() throws MessagingException{
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.setProperty("mail.imap.starttls.enable", "true");
props.setProperty("mail.imap.ssl.enable", "true");
try{
Session session = Session.getInstance(props, null);
session.setDebug(true);
Store store = session.getStore("imaps");
store.connect("mailbox.xyz.com", "emailId@xyz.com", "password");
System.out.println(store);
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());
}
catch (NoSuchProviderException e){
e.printStackTrace();
System.exit(1);
}
}
这是调试输出以及执行上述代码时引发的错误。奇怪的是,相同的代码在同一网络中的其他一些系统中也能工作。
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "imap.gmail.com", port 993, isSSL true
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
奇怪的是,这段代码不知何故可以在其他一些系统上运行。