title: Java - SMTP Transporter requires SocketOutputStream to be open indefinitely
body: <p>During a refactoring job, I discovered that if I close my outputstream using "try-with-resources" - I always get a <code>MessagingException</code> from java's SMTPTransport. It always complains that the socket was closed.</p>
  
  <p>The code which I identified to have the problems is this:</p>
  
  <pre><code>try (LineOutputStream los = new LineOutputStream(os);) {
      los.writeln(signatureHeaderLine);
      Enumeration hdrLines = getNonMatchingHeaderLines(ignoreList);
      while (hdrLines.hasMoreElements()) {
          String notIgnoredLine = (String) hdrLines.nextElement();
          los.writeln(notIgnoredLine);
      }
  
      los.writeln();
      // Send signed mail to waiting DATA command
      os.write(osBody.toByteArray());
      os.flush();
  } catch (MessagingException me) {
      // Deal with it
  } catch (Exception e) {
      // Deal with it
  }
  </code></pre>
  
  <p>The code above is part of an override of <code>MimeMessage.writeTo(OutputStream, String[])</code> And the issue comes when `issueSendCommand' and 'sendCommand' is eventually called from <a href="http://kickjava.com/src/com/sun/mail/smtp/SMTPTransport.java.htm" rel="nofollow noreferrer">SMTPTransport</a>.</p>
  
  <p>So does this mean my sockets should remain open all the time? I know from non-technical viewpoint, it doesn't feel right to close the socket since I will be writing messages through it. But I was trying to understand whether this would cause memory leak by any chance in the future.</p>
  
  <p>Regards,</p>