title: Is it acceptable to leave the javamail session Transport open?
body: <p>My application needs to send emails on an ad hoc basis.  I'm using javamail's getDefaultSession and getTransport to send the message, and it's all working as expected.</p>
  
  <p>However I've noticed that the send can take a long time - up to seven seconds per send.  If I break the steps down, like this:</p>
  
  <pre><code>Transport transport = session.getTransport("smtp");
  transport.connect();
  transport.sendMessage( msg, addresses )
  transport.close();
  </code></pre>
  
  <p>...I can see that it's the connect() call that takes almost all of the time, each time.</p>
  
  <p>All of the examples I've found do this - get a transport, connect, send, disconnect.  But of course they're all single-shot examples, or sending large batches of emails in a single call.</p>
  
  <p>I was thinking I could just leave the connection open, like this:</p>
  
  <pre><code>Transport transport = session.getTransport("smtp");
  if (!transport.isConnected())
      transport.connect();
  transport.sendMessage( msg, addresses )
  </code></pre>
  
  <p>(There's a variation on that, here: <a href="https://stackoverflow.com/questions/2358569/java-mail-keeping-transport-object-connected">java mail keeping Transport object connected</a>).</p>
  
  <p>I'll have to close it eventually, in a shutdown hook of some kind.  And I might have to have a fallback (if the connection has been lost but the transport doesn't realise).  But is there any reason not to just leave it open like this during the application lifetime?</p>
  
  <p>Thanks,
  Alastair</p>