gpt4 book ai didi

jakarta-mail - javax.mail.MessagingException : Connection reset while trying to access gmail using java mail API

转载 作者:行者123 更新时间:2023-12-04 16:50:48 26 4
gpt4 key购买 nike

我试图使用 java 邮件 API 阅读我的 gmail 帐户中的邮件。这是代码:

import java.util.*;
import java.io.*;
import java.awt.*;
import javax.mail.*;
import javax.mail.search.FlagTerm;
import javax.mail.Flags.Flag;
public class MailPharser {
/**
* @param args
*/
public void mailRead()
{
Folder inbox;
// TODO Auto-generated method stub
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try
{
Session session = Session.getDefaultInstance(props, null);

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

store.connect("imap.gmail.com","<mailid@gmail.com>", "<password>");
inbox = store.getFolder("Inbox");
System.out.println("No of Unread Messages : " + inbox.getUnreadMessageCount());


}
catch (Exception ex)
{ System.out.println("Error caught"); ex.printStackTrace(); }

}

public static void main(String[] args) {
MailPharser mp = new MailPharser();
mp.mailRead();
}

}

运行时出现以下错误:

javax.mail.MessagingException: Connection reset;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:670)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at MailPharser.mailRead(MailPharser.java:26)
at MailPharser.main(MailPharser.java:40)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(Unknown Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
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:233)
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)
... 4 more

我添加了跟踪,发现连接调用失败了。我做错了什么吗?任何帮助都可以。提前致谢...

最佳答案

这可能是与 JavaMail 无关的网络问题,例如,您和 Gmail 之间的代理或防火墙。

如果你想知道是你的代码坏了还是网络坏了,你可以test using the code that comes with JavaMail .如果 JavaMail 代码有效,那么您就知道您的代码有问题,您可以使用常见问题解答中的 JavaMail 代码来改进您的代码。

如果发现是网络问题,JavaMail FAQ 也有tips for debugging it further .

关于jakarta-mail - javax.mail.MessagingException : Connection reset while trying to access gmail using java mail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10008947/

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