title: Java Mail api, connection gets lost during message process
body: <p>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. </p>
  
  <pre><code>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();
  
      ..
  }
  </code></pre>
  
  <p>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:</p>
  
  <ul>
  <li><p>AFAIK, the mail server can sometimes reject new connections, but does
  it terminate current living connections (may be becasue of too much
  connections or disconnects old ones to give access to the new ones?)</p></li>
  <li><p>When the store is disconnected, does the folder gets closed too?</p></li>
  <li><p>Is it better to check the folder?</p></li>
  <li><p>The connection may be lost everywhere in the for loop and it does not
  seem to be a good practise to put isConnected check everywhere in the
  loop, it will make the code dirty and also cause performance issues,
  is it a good practise to put in a try catch block and check for
  IOExceptions? (Folder closed) Or other suggestions? Which exceptions
  should be handled? There may be some cases where the message is not
  parseable but connection is healthy.</p></li>
  <li><p>What about adding a disconnect listener?</p></li>
  </ul>