This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。





如何配置Java应用程序以将993端口与TLS结合使用,以从IMAP邮箱中读取邮件。

我将邮件服务器的证书存储在默认的javakeystore cacerts中。
尝试连接到邮箱后。我得到了这个堆栈跟踪(完整的跟踪):

 javax.mail.MessagingException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty;
 nested exception is:
    javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at imap.IMAP.receive(IMAP.java:77)
    at imap.IMAP.main(IMAP.java:46)
 Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:190)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1764)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1725)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1708)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1237)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1214)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:548)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:352)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:207)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:113)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:111)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:637)
    ... 5 more
Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:57)
    at sun.security.validator.Validator.getInstance(Validator.java:161)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.getValidator(X509TrustManagerImpl.java:108)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:204)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136)
    at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Handshaker.java:593)
    at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:958)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1203)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1230)
    ... 12 more
Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:183)
    at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:103)
    at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:87)
    at sun.security.validator.PKIXValidator.<init>(PKIXValidator.java:55)
    ... 23 more
15.03.2013 13:54:50 imap.IMAP main
SCHWERWIEGEND: null
java.lang.NullPointerException
    at imap.IMAP.receive(IMAP.java:123)
    at imap.IMAP.main(IMAP.java:46)


这是我的比例:

System.setProperty("javax.net.ssl.trustStore", "/usr/java/jdk_1.6.0_35/jre/lib/security/cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");

Properties props = System.getProperties();

props.setProperty("mail.imaps.host", server);
props.setProperty("mail.imaps.user", user);
props.setProperty("mail.imaps.password", password);
props.setProperty("mail.imaps.auth", "true");
props.setProperty("mail.imaps.starttls.enable", "true");
props.setProperty("mail.imaps.socketFactory.port", "993");
props.setProperty("mail.imaps.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imaps.socketFactory.fallback", "false");

MailAuthenticator auth = new MailAuthenticator(user, password);

Session session = Session.getDefaultInstance(props, auth);

store = session.getStore("imaps");
store.connect();


缺少什么吗?互联网上有很多关于此问题的文章。但是大多数是用于SMTP。我还没有找到真正可以解释的东西,要使此案例正常工作,您真正需要哪些属性。

我希望有人可以帮助我找到我的错误。

非常感谢。

最佳答案

首先是一些注释,这些注释不是造成问题的原因,但是可以简化程序。

摆脱socketFactory属性,您不需要它们。

使用Session.getInstance代替Session.getDefaultInstance。

摆脱MailAuthenticator并调用store.connect(用户,密码)。

有关详细信息,请参见JavaMail FAQ

至于您得到的例外情况,这似乎是与您的证书或信任库有关的问题。 This JavaMail FAQ entry具有调试此类问题的提示的指针。您可能需要Java SSL /网络/安全专家来解释问题所在。

您说您已经将证书添加到信任库中,但是以防万一您可能想检查this JavaMail FAQ entry

关于java - 如何配置“imaps”来接收邮件? JavaMail ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15433992/

10-14 11:09