开发者

JavaMail - why do I sporadically get a StoreClosedException on a folder operation?

开发者 https://www.devze.com 2023-02-15 05:14 出处:网络
I have the following code fragment: 开发者_运维百科// using classes from javax.mail.* // Session / Store setup code

I have the following code fragment:

开发者_运维百科
// using classes from javax.mail.*
// Session / Store setup code
// Store implementation class = com.sun.mail.imap.IMAPStore

Folder folder = store.getFolder("INBOX");  // store setup previously
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
Folder anotherFolder = store.getFolder("F1");
if ( !anotherFolder.exists()) {                   [***]
  anotherFolder.create(Folder.HOLDS_MESSAGES);
}
folder.copyMessages(messages, anotherFolder);

Occasionally, I will get the following exception:

javax.mail.StoreClosedException: failed to create new store connection
   at com.sun.mail.imap.IMAPFolder.throwClosedException(IMAPFolder.java:2208)
   at com.sun.mail.imap.IMAPFolder.doCommand(IMAPFolder.java:2335)
   at com.sun.mail.imap.IMAPFolder.exists(IMAPFolder.java:427)
   at [***]

I am not sure if this is a result of me using the JavaMail API incorrectly, or if this is a server problem. I have two observations:

  1. The failure occurs when it checks the folder's existence, rather than when the getFolder is invoked.

  2. getFolder is invoked more than once in the code.

I suppose it is possible that the connection to the mail server dies at some point after the folder.getMessages() from time to time, but it ALWAYS fails on the exist() call as opposed to the create() call.

One solution is to call store.isConnected() first and reconnect if required, but I want to figure out if there is something I am doing wrong first before I resort to that.

I would appreciate any insight or advice on how to probe deeper into the source of this exception. Thanks in advance!


The issue is you are not closing the folder . After usage we must close the folder. Again open the folder once needed. Don't open the same folder many times.

folder.close(false);
.
.
.
.
.
.
if (!folder.isOpen()) {
    folder.open(Folder.READ_ONLY);
}


I traced it down to the server itself. I was connecting to Dovecot with a recursive method to copy the whole folder structure.

(Basically, I am forking ImapCopy from its 2008 unmaintained version relying on old Log4J and JGoodies to use native Swing/Netbeans forms, and newer Log4j, and also to support servers that don't allow folders&messages intermixed)

It was dovecot itself: 2016-06-04T01:48:34.538050-04:00 moz dovecot: imap-login: Maximum number of connections from user+IP exceeded (mail_max_userip_connections=10): user=, method=PLAIN, rip=10.1.4.21, lip=10.1.4.235, TLS, session=<6MXUY200UQAKrgQV>

So now I have to update that field to allow more than 10 connections. Or, more properly because I am planning on using it on o365, somehow read the folder structures first recursively (without opening new connections, or closing old ones) and then cycling through the folder structure separately to handle the messages.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号