gpt4 book ai didi

java - 如何配置 "imaps"接收邮件? Java邮件

转载 作者:行者123 更新时间:2023-12-01 14:45:59 26 4
gpt4 key购买 nike

如何配置我的 Java 应用程序以使用带有 TLS 的端口 993 来读取来自 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(user, password)。

请参阅JavaMail FAQ了解详情。

至于您遇到的异常,这似乎是与您的证书或信任存储相关的一些问题。 This JavaMail FAQ entry提供了调试此类问题的提示。您可能需要 Java SSL/网络/安全专家来解释问题所在。

您说您已经将证书添加到信任存储区,但以防万一您可能想检查 this JavaMail FAQ entry .

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com