Javamail与Gmail的IMAP连接在随机持续时间后会丢失

前端之家收集整理的这篇文章主要介绍了Javamail与Gmail的IMAP连接在随机持续时间后会丢失前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用Javamail对gmail imap服务器进行身份验证,并一次获取一个完整邮箱的邮件头并在本地编制索引.对于消息很少的收件箱,这很好用.对于邮件通常超过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();
                        }
                    } 
原文链接:https://www.f2er.com/java/437733.html

猜你在找的Java相关文章