title: JavaMail: "Remote host closed connection during handshake" while connecting with office365
body: <p>I try to connect to office365 mail server with javaMail v1.5.3 (application is deployed on tomcat 6). Im running a thread on startup that is checking for new emails in a loop with one minute sleep. In most cases connection is successfully estabilished and everything works just fine but sometimes I get 
  "Remote host closed connection during handshake" error. </p>
  
  <p>The error is caused by </p>
  
  <pre><code>Caused by: java.io.EOFException: SSL peer shut down incorrectly
  at sun.security.ssl.InputRecord.read(InputRecord.java:482)
  at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
  </code></pre>
  
  <p>I have tried sulutions from: 
  <a href="https://stackoverflow.com/questions/15589880/how-to-make-java-6-which-fails-ssl-connection-with-ssl-peer-shut-down-incorrec">How to make Java 6, which fails SSL connection with &quot;SSL peer shut down incorrectly&quot;, succeed like Java 7?</a>
  and <a href="https://stackoverflow.com/questions/11980218/javax-net-ssl-sslhandshakeexception-remote-host-closed-connection-during-handsh">javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake</a> :</p>
  
  <p>adding <code>-Dhttps.protocols=TLSv1,SSLv3</code> and -<code>Dsun.security.ssl.allowUnsafeRenegotiation=true</code> to my tomcat environment 
  but I didn't get any result. Still - randomly exception occurs.</p>
  
  <p>I enabled debug mode on javax.net and on IMAP connection and get results:<br>
  when connection is not estabilishing correctly log looks like this :</p>
  
  <pre><code> DEBUG: setDebug: JavaMail version 1.5.3
   DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
   DEBUG IMAP: mail.imap.fetchsize: 16384
   DEBUG IMAP: mail.imap.ignorebodystructuresize: false
   DEBUG IMAP: mail.imap.statuscachetimeout: 1000
   DEBUG IMAP: mail.imap.appendbuffersize: -1
   DEBUG IMAP: mail.imap.minidletime: 10
   DEBUG IMAP: closeFoldersOnStoreFailure
   DEBUG IMAP: trying to connect to host "outlook.office365.com", port 993, isSSL true
   Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA
   Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256
   Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256
   Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256
   Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
   Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
   Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA
   Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA
   Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256
   Allow unsafe renegotiation: false
  Allow legacy hello messages: true
  Is initial handshake: true
  Is secure renegotiation: false
   %% Client cached [Session-4, TLS_RSA_WITH_AES_128_CBC_SHA]
   %% Try resuming [Session-4, TLS_RSA_WITH_AES_128_CBC_SHA] from port 51400
   *** ClientHello, TLSv1
   RandomCookie:  
   GMT: 1435130635 
   bytes = { , , , , , , , , , , , , , , , , , , , , , , , , , ,  }
   Session ID:  {66, 20, 0, 0, 123, 9, 142, 72, 150, 39, 215, 34, 63, 169, 129, 23, 25, 182, 88, 196, 86, 27, 216, 191, 117, 196, 37, 118, 229, 8, 9, 64}-
   Cipher Suites: [TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_EMPTY_RENEGOTIATION_INFO_SCSV]-
   Compression Methods:  { }
   Extension server_name, server_name: [host_name: outlook.office365.com]
   ***-
   [write] MD5 and SHA1 hashes:  len = 125
   46@CheckMailThread, WRITE: TLSv1 Handshake, length = 125
   [Raw write]: length = 130
   46@CheckMailThread, received EOFException: error
   46@CheckMailThread, handling exception: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
   46@CheckMailThread, SEND TLSv1 ALERT:  fatal, description = handshake_failure
   46@CheckMailThread, WRITE: TLSv1 Alert, length = 2
   [Raw write]: length = 7
   46@CheckMailThread, called closeSocket()
  </code></pre>
  
  <p>and then exception occurs</p>
  
  <pre><code>javax.mail.MessagingException: Remote host closed connection during handshake;
    nested exception is:
      javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:733)
      at javax.mail.Service.connect(Service.java:364)
      at javax.mail.Service.connect(Service.java:245)
      (...)
  Caused by: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:953)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
      at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
      at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:574)
      at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:369)
      at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:236)
      at com.sun.mail.iap.Protocol.&lt;init&gt;(Protocol.java:117)
      at com.sun.mail.imap.protocol.IMAPProtocol.&lt;init&gt;(IMAPProtocol.java:120)
      at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:753)
      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:696)
      ... 6 more
  Caused by: java.io.EOFException: SSL peer shut down incorrectly
      at sun.security.ssl.InputRecord.read(InputRecord.java:482)
      at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
      ... 16 more
  </code></pre>
  
  <p>In other hand in most cases thread is doing ok and the log looks like this: </p>
  
  <pre><code>DEBUG: setDebug: JavaMail version 1.5.3 
  DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle] 
  DEBUG IMAP: mail.imap.fetchsize: 16384 
  DEBUG IMAP: mail.imap.ignorebodystructuresize: false 
  DEBUG IMAP: mail.imap.statuscachetimeout: 1000 
  DEBUG IMAP: mail.imap.appendbuffersize: -1
   DEBUG IMAP: mail.imap.minidletime: 10 
  DEBUG IMAP: closeFoldersOnStoreFailure 
  DEBUG IMAP: trying to connect to host "outlook.office365.com", port 993, isSSL true 
  Ignoring unavailable cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA 
  Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_128_CBC_SHA256 
  Ignoring unsupported cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA256 
  Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA256 
  Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 
  Ignoring unsupported cipher suite: TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 
  Ignoring unavailable cipher suite: TLS_DHE_DSS_WITH_AES_256_CBC_SHA 
  Ignoring unavailable cipher suite: TLS_RSA_WITH_AES_256_CBC_SHA 
  Ignoring unsupported cipher suite: TLS_RSA_WITH_AES_128_CBC_SHA256 
  Allow unsafe renegotiation: false
  Allow legacy hello messages: true
  Is initial handshake: true
  Is secure renegotiation: false 
  %% Client cached [Session-150, TLS_RSA_WITH_AES_128_CBC_SHA]
   %% Try resuming [Session-150, TLS_RSA_WITH_AES_128_CBC_SHA] from port 59183
   *** ClientHello, TLSv1 
  RandomCookie:   
  GMT: 1435076193  
  bytes = { , , , , , , , , , , , , , , , , , , , , , , , , , , , }  
  Session ID:   
  {241, 61, 0, 0, 224, 114, 43, 139, 255, 64, 232, 7, 209, 90, 5, 63, 63, 117, 33, 66, 215, 35, 48, 83, 131, 211, 38, 151, 73, 232, 6, 120} 
  Cipher Suites: [TLS_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_RSA_WITH_AES_128_CBC_SHA, TLS_DHE_DSS_WITH_AES_128_CBC_SHA, SSL_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA, SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA, SSL_RSA_WITH_RC4_128_SHA, SSL_RSA_WITH_RC4_128_MD5, TLS_EMPTY_RENEGOTIATION_INFO_SCSV] 
  Compression Methods:  {  
   } 
  Extension server_name, server_name: [host_name: outlook.office365.com] 
  *** 
  [write] MD5 and SHA1 hashes:  len = 125 
  46@CheckMailThread, WRITE: TLSv1 Handshake, length = 125 
  [Raw write]: length = 130 
  [Raw read]: length = 5 
  [Raw read]: length = 3532 
  46@CheckMailThread, READ: TLSv1 Handshake, length = 3532 
  *** ServerHello, TLSv1 
  RandomCookie:   
  GMT: 1435076194  
  Bytes = { , , , , , , , , , , , , , , , , , , , , , , , , , , , } 
  Session ID:   
  {112, 39, 0, 0, 59, 34, 200, 120, 31, 23, 110, 30, 10, 37, 236, 213, 46, 233, 201, 3, 253, 223, 81, 109, 188, 218, 33, 164, 33, 127, 27, 55} 
  Cipher Suite: TLS_RSA_WITH_AES_128_CBC_SHA 
  Compression Method: 0 
  Extension renegotiation_info, renegotiated_connection: &lt;empty&gt; 
  *** 
  %% Initialized:  [Session-151, TLS_RSA_WITH_AES_128_CBC_SHA]
   ** TLS_RSA_WITH_AES_128_CBC_SHA 
  [read] MD5 and SHA1 hashes:  len = 81 
  *** Certificate chain (...)
  </code></pre>
  
  <p>And then goes the certificate etc </p>
  
  <p>So I was wonderig what can cause such inconsistent behaviour. </p>