I'm using Javamail to authenticate into gmail imap server and fetch messages headers for complete mailbox one at a time and index locally. For inboxes which have few messages, this works fine. For inboxes with messages more than 10k , javamail imap connection gets dropped (possibly by server). This duration is also random.

Since I am continuously fetch message headers one after one there is a time where the connection is idle for a long time, so it shouldn't be a timeout.

Java Code:
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());
    }
Error Stack:
Java Code:
   [java] javax.mail.FolderClosedException: * BYE JavaMail Exception: java.io.IOException: [B]Connection dropped by server?[/B]
     [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)
Any help is appreciated. Thanks !