title: Error sending SMTP message from Tomcat: No buffer space available
body: <p>I'm Running <strong>Tomcat7</strong> on a server (right now it's a virtual machine but should be configured as unlimited connexions).</p>
  
  <p>I have connexions with a SMTP server and also with facebook Servers. 
  My traffic is around 250hits/mins.</p>
  
  <p>I'm running djabberd with around a few hundreds connected users average time.</p>
  
  <p>Sometimes I get :</p>
  
  <pre><code>javax.mail.MessagingException: Could not connect to SMTP host: xxx.xxx.xxx.xxx, port: xxxxx;
    nested exception is:
      java.net.SocketException: No buffer space available
      at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
      at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
      at javax.mail.Service.connect(Service.java:295)
      at javax.mail.Service.connect(Service.java:176)
      at javax.mail.Service.connect(Service.java:125)
      at javax.mail.Transport.send0(Transport.java:194)
      at javax.mail.Transport.send(Transport.java:124)
      at xxxxxxxxxxxxxxxx.controllers.EmailManager.sendEmail(EmailManager.java:151)
  </code></pre>
  
  <p>Or same error connecting to facebook servers.</p>
  
  <pre><code>netstat -n | wc -l 
  348
  </code></pre>
  
  <p>Tomcat can accept acceptCount="1024" connexions.</p>
  
  <p>Where should I investigate ?</p>
  
  <p><strong>[UPDATE 1]</strong></p>
  
  <p>Send an email :</p>
  
  <pre><code>Context envContext = (Context) initContext.lookup("java:/comp/env");
  session = (javax.mail.Session) envContext.lookup("mail/emailSession");
  
  MimeMessage message = new MimeMessage(session);
  //....
  Transport.send(message);
  </code></pre>
  
  <p>Configuration :</p>
  
  <pre><code>&lt;resource-ref&gt;
          &lt;description&gt;
              JNDI javamail session resource reference
          &lt;/description&gt;
          &lt;res-ref-name&gt;mail/emailSession&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.mail.Session&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-ref&gt;
  </code></pre>
  
  <p>Facebook connexion :</p>
  
  <pre><code>FacebookClient facebookClient = new DefaultFacebookClient(accessToken);
  User user = facebookClient.fetchObject("me", User.class);
  </code></pre>