gpt4 book ai didi

Javamail IMAP 与 Gmail 的连接在随机持续时间后断开

转载 作者:搜寻专家 更新时间:2023-10-31 20:15:57 27 4
gpt4 key购买 nike

我正在使用 Javamail 对 gmail imap 服务器进行身份验证,一次获取一个完整邮箱的邮件 header 并在本地建立索引。对于消息很少的收件箱,这很好用。对于一般消息超过 10k 的收件箱,javamail imap 连接会断开(可能由服务器断开)。需要注意的是,这个持续时间是随机的。

由于我一直在一个接一个地获取消息头,所以没有连接长时间空闲的时间,所以它不可能超时。

----------------------------代码---------------- ------------------

IMAPSSLStore imapSslStore = connectToImap("imap.googlemail.com", 993, email, oauthToken,  oauthTokenSecret, getMyConsumer(), true);
System.out.println("Successfully authenticated to IMAP.\n");
Folder inbox = imapSslStore.getFolder("[Gmail]/All Mail");
inbox.open(Folder.READ_ONLY);

Message messages[] = inbox.getMessages();

for(Message message:messages) {
System.out.println(message);
System.out.println("MSG NO:"+message.getMessageNumber());
System.out.println("FROM:"+message.getFrom().toString());
}

----------------------------代码---------------- ------------------------------

异常跟踪:

[java] javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: **Connection dropped by server?**

[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:194)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:747)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:201)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:104)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:597)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:357)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:698)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:199)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)
[java] Caused by: javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
[java] at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1198)
[java] at com.sun.mail.imap.IMAPMessage.getFrom(IMAPMessage.java:233)
[java] at com.google.code.samples.xoauth.XoauthAuthenticator.main(XoauthAuthenticator.java:209)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:597)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
[java] ... 20 more
[java] --- Nested Exception ---
[java] javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
[java] at com.sun.mail.imap.IMAPMessage.loadEnvelope(IMAPMessage.java:1198)
[java] at com.sun.mail.imap.IMAPMessage.getFrom(IMAPMessage.java:233)
[java] at com.google.code.samples.xoauth.XoauthAuthenticator.main(XoauthAuthenticator.java:209)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:597)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.run(ExecuteJava.java:217)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:152)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:747)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:201)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:104)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:288)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[java] at java.lang.reflect.Method.invoke(Method.java:597)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:105)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:357)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:385)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
[java] at org.apache.tools.ant.Main.runBuild(Main.java:698)
[java] at org.apache.tools.ant.Main.startAnt(Main.java:199)
[java] at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
[java] at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

我正在使用独立版本的 javamail (1.4.3) 我已经努力解决这个问题一段时间了。感谢您的帮助。

最佳答案

您需要捕获 FolderClosedException,然后再次尝试连接。我遇到过这个问题并通过以下代码解决了它:

 catch (FolderClosedException ex) {
ex.printStackTrace();
ConnectPop3(Username, Password);
if (!folder.isOpen()) {
folder.open(Folder.READ_ONLY);
messages = folder.getMessages();
}
}

关于Javamail IMAP 与 Gmail 的连接在随机持续时间后断开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3948838/

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