Priyan Perera
Priyan Perera

Reputation: 479

JavaMail : FolderClosedException coming frequently

I'm using java mail to connect with gmail and I'm keeping one store for the all actions. (Store is set to static.). And the IMAPFolder instances are attached with imap listeners. So the folders are kept always open. (Folder close is not called any time) But while running after few minutes I'm getting FolderClosedException. After that exception, though the folder can be reopened but the idle() command cannot be issued again, which will result in NullPointerException.

Is there any wrong with keeping folders open always?

Thanks in advance.

===================================================================

[Edit] Here I'm pasting the actual code i'm doing POC with. The NullPointerException comes when I check .isConnected() after reconnecting the store. Below is the run method of Thread which sends idle() command to the store.

public void run() {
        while (true) {
            try {
                System.out.println("Checking connectivity...");

                if (store.isConnected()) {
                    store.idle();
                    System.out.println("IDLE send...");
                } else {
                    Thread.sleep(5000);
                    System.out.println("Tring to connect...");

                    //Trying to reconnect to the store.
                    store.connect();
                    System.out.println("Previous store connected again");
                }
            } catch (InterruptedException ex) {
                System.out.println("InterruptedException...");
            } catch (StoreClosedException ex) {
                System.out.println("StoreClosedException...");
            } catch (MessagingException ex) {
                System.out.println("MessagingException...");
            }
        }
    }

Here is the stack trace:

Exception in thread "Thread-1" java.lang.NullPointerException
at com.sun.mail.imap.IMAPStore.waitIfIdle(IMAPStore.java:1881)
at com.sun.mail.imap.IMAPStore.getStoreProtocol(IMAPStore.java:946)
at com.sun.mail.imap.IMAPStore.isConnected(IMAPStore.java:1347)
at pocworks.POCWorks1$IDLEThread.run(POCWorks1.java:125)

Upvotes: 3

Views: 4302

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

Generally, mail servers don't like you to keep connections open when you're not using them. Typical IMAP servers will give you 30 minutes before they time out an unused connection; Gmail may be more aggressive.

Upvotes: 3

Related Questions