JavaMail - why do I sporadically get the StoreClosedException property when working with a folder?

I have the following code snippet:

// 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); 

Sometimes I 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 the result of improper use of the JavaMail API or a server problem. I have two observations:

  • A crash occurs when it checks for the existence of a folder, and not when getFolder is called.

  • getFolder is called more than once in code.

I believe it is possible that the connection to the mail server dies from time to time with the name folder.getMessages (), but it ALWAYS fails when calling exist (), and not in calling create ().

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

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

+4
source share
2 answers

The problem is that you are not closing the folder. After use, we must close the folder. Reopen the folder again when necessary. Do not open the same folder many times.

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

I traced it to the server itself. I connected to Dovecot using a recursive method to copy the entire folder structure.

(Basically, I run ImapCopy from my unsupported version of 2008, relying on the old Log4J and JGoodies to use native Swing / Netbeans forms and the newer Log4j, as well as to support servers that prevent you from moving between messages and messages)

It was dovecot itself: 2016-06-04T01: 48: 34.538050-04: 00 moz dovecot: imap-login: the maximum number of connections from the user + IP was 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 this field to allow more than 10 connections. Or, more correctly, because I plan to use it on o365, somehow I first look through the folder structures recursively (without opening new connections or closing the old ones), and then cycle through the folder structure for message processing.

+1
source

Source: https://habr.com/ru/post/1342228/


All Articles