benchpresser
benchpresser

Reputation: 2188

Java Mail api, connection gets lost during message process

In our java mail (using Java Mail API) application we first connect to the mail server, fetch messages, process headers and then afterwards process the message bodies and attachments using pop3 as usual.

Session session = Session.getInstance(props, null);
Store store = session.getStore(urln);
store.connect();
Folder f = store.getFolder("INBOX");
f.open(READ);
Messages m = f.getMessages(..);
for (Message m : messages) {
    if (!store.isConnected()) {
        //raise exception
    }
    processSubject();

    processFrom();

    processBodyAndAttachments();

    ..
}

The implementation works fine on most environments, but on some customer the storeconnection gets lost during the process in the for loop. We can see the raises exception in the logs. My questions:

Upvotes: 2

Views: 539

Answers (1)

Bill Shannon
Bill Shannon

Reputation: 29971

  • Network connections can be broken for a variety of reasons. Your program always has to be prepared for the connection to drop at any time.
  • With POP3, there is only one connection, so if the connection is dropped the store should be disconnected and the folder should be closed.
  • If the Folder is open, check the Folder. Otherwise check the Store.
  • You need a strategy for handling failures. If you keep track of what messages have been successfully processed you may be able to restart the processing at the next message after a failure. A lot of the details depend on your environment and your application requirements.
  • A disconnect listener won't make this easier.

Upvotes: 3

Related Questions