title: Java Mail timeout - javax.mail timeout
body: <p>I have an application which is being updated to run inside a docker container, and everything is working as well, but now I am facing a trouble with the javax.mail.</p>
  <p>I am getting a timeout error.</p>
  <p>I have increased the timeout by 7000 to 120000 milliseconds and still getting same error.</p>
  <p>Here is the method that create a mail session.</p>
  <pre class="lang-java prettyprint-override"><code>public static Session getSession(String user, String pass, String host, String port) {
                  
          Session sessao = null;
          Properties properties = new Properties();
          properties.setProperty(&quot;mail.smtp.host&quot;, host);
          properties.setProperty(&quot;mail.smtp.port&quot;, port);
          properties.setProperty(&quot;mail.smtp.socketFactory.port&quot;, port);
          properties.setProperty(&quot;mail.smtp.connectiontimeout&quot;, &quot;120000&quot;);
          properties.setProperty(&quot;mail.smtp.timeout&quot;, &quot;120000&quot;);
          properties.setProperty(&quot;mail.smtp.writetimeout&quot;, &quot;120000&quot;);
          if (pass != null &amp;&amp; !pass.isEmpty()) {
              Authenticator authenticator = new Authenticator(user, pass);
              properties.setProperty(&quot;mail.smtp.submitter&quot;, authenticator.getPasswordAuthentication().getUserName());
              properties.setProperty(&quot;mail.transport.protocol&quot;, &quot;smtps&quot;);
              // TLS PORT = 587
              if(&quot;587&quot;.equals(port)) {
                  properties.setProperty(&quot;mail.smtp.starttls.enable&quot;, &quot;true&quot;);
              } else {
                  properties.setProperty(&quot;mail.smtp.ssl.enable&quot;, &quot;true&quot;);
              }
              properties.setProperty(&quot;mail.smtp.ssl.trust&quot;, &quot;*&quot;);
              properties.setProperty(&quot;mail.smtp.ssl.protocols&quot;, &quot;TLSv1.2&quot;);
              properties.setProperty(&quot;mail.smtp.auth&quot;, &quot;true&quot;);
              properties.setProperty(&quot;mail.smtp.socketFactory.class&quot;, &quot;javax.net.ssl.SSLSocketFactory&quot;);
              sessao = Session.getInstance(properties, authenticator);
          } else {
              properties.setProperty(&quot;mail.smtp.auth&quot;, &quot;false&quot;);
              sessao = Session.getInstance(properties);
          }
          sessao.setDebug(true);
          System.out.println(&quot;#########################################&quot;);
          return sessao;
      }
  
  </code></pre>
  <p>And here is my error and a debug log:</p>
  <pre><code>line 1 column 1 - Warning: unknown attribute &quot;contenttype&quot;
  line 1 column 1 - Warning: missing &lt;!DOCTYPE&gt; declaration
  line 93 column 8 - Warning: inserting missing 'title' element
  InputStream: Document content looks like XHTML 1.0 Strict
  3 warnings, no errors were found!
  DEBUG: setDebug: JavaMail version 1.6.1
  #########################################
  DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: useEhlo true, useAuth true
  DEBUG SMTP: trying to connect to host &quot;smtp.gmail.com&quot;, port 587, isSSL false
  220 smtp.gmail.com ESMTP EXEMPLEMAIL.37 - gsmtp
  DEBUG SMTP: connected to host &quot;smtp.gmail.com&quot;, port: 587
  
  EHLO EXEMPLEMAIL
  250-smtp.gmail.com at your service, [A IP]
  250-SIZE 35882577
  250-8BITMIME
  250-STARTTLS
  250-ENHANCEDSTATUSCODES
  250-PIPELINING
  250-CHUNKING
  250 SMTPUTF8
  DEBUG SMTP: Found extension &quot;SIZE&quot;, arg &quot;35882577&quot;
  DEBUG SMTP: Found extension &quot;8BITMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;STARTTLS&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;ENHANCEDSTATUSCODES&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;PIPELINING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;CHUNKING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;SMTPUTF8&quot;, arg &quot;&quot;
  STARTTLS
  220 2.0.0 Ready to start TLS
  EHLO EXEMPLEMAIL
  250-smtp.gmail.com at your service, [THAT IP]
  250-SIZE 35882577
  250-8BITMIME
  250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
  250-ENHANCEDSTATUSCODES
  250-PIPELINING
  250-CHUNKING
  250 SMTPUTF8
  DEBUG SMTP: Found extension &quot;SIZE&quot;, arg &quot;35882577&quot;
  DEBUG SMTP: Found extension &quot;8BITMIME&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;AUTH&quot;, arg &quot;LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH&quot;
  DEBUG SMTP: Found extension &quot;ENHANCEDSTATUSCODES&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;PIPELINING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;CHUNKING&quot;, arg &quot;&quot;
  DEBUG SMTP: Found extension &quot;SMTPUTF8&quot;, arg &quot;&quot;
  DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
  DEBUG SMTP: AUTH LOGIN command trace suppressed
  DEBUG SMTP: AUTH LOGIN succeeded
  DEBUG SMTP: use8bit false
  MAIL FROM:&lt;EXEMPLEMAIL@EXEMPLEMAIL.adm.br&gt; AUTH=MAIL@EXEMPLEMAIL.adm.br
  250 2.1.0 OK n3sm3133302ili.37 - gsmtp
  RCPT TO:&lt;EXEMPLEMAIL@EXEMPLEMAIL.com.br&gt;
  250 2.1.5 OK EXEMPLEMAIL.37 - gsmtp
  DEBUG SMTP: Verified Addresses
  DEBUG SMTP:   &quot;EXEMPLEMAIL.EXEMPLEMAIL@EXEMPLEMAIL.com.br&quot; &lt;EXEMPLEMAIL.EXEMPLEMAIL@EXEMPLEMAIL.com.br&gt;
  DATA
  354  Go ahead EXEMPLEMAIL.37 - gsmtp
  QUIT
  DEBUG SMTP: exception reading response, THROW: 
  java.net.SocketTimeoutException: Read timed out
      at java.base/java.net.SocketInputStream.socketRead0(Native Method)
      at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
      at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
      at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
      at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:478)
      at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:472)
      at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:70)
      at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1364)
      at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:973)
      at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:124)
      at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:252)
      at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:271)
      at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:89)
      at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2184)
      at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1212)
      at javax.mail.Transport.send0(Transport.java:257)
      at javax.mail.Transport.send(Transport.java:124)
      at br.com.webcard.gestaofrota.util.EmailUtil.sendEmailWithFiles(EmailUtil.java:104)
      at
  
  // Those methods just call the method above, so you can ignore.
   br.com.webcard.gestaofrota.view.util.ReciboUtil.enviarReciboPorEmail(ReciboUtil.java:74)
      at br.com.webcard.gestaofrota.view.comum.VendaView.enviarReciboPorEmail(VendaView.java:447)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at javax.el.BeanELResolver.invoke(BeanELResolver.java:152)
      at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:79)
      at com.sun.el.parser.AstValue.invoke(AstValue.java:228)
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
      at javax.faces.component.UICommand.broadcast(UICommand.java:315)
      at javax.faces.component.UIData.broadcast(UIData.java:1108)
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
      at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:155)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
      at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
      at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
      at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:347)
      at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:263)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:115)
      at org.springframework.boot.web.support.ErrorPageFilter.access$000(ErrorPageFilter.java:59)
      at org.springframework.boot.web.support.ErrorPageFilter$1.doFilterInternal(ErrorPageFilter.java:90)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at org.springframework.boot.web.support.ErrorPageFilter.doFilter(ErrorPageFilter.java:108)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
      at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
      at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
      at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
      at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
      at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
      at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
      at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
      at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.base/java.lang.Thread.run(Thread.java:829)
  2021-10-21 14:11:17.450  WARN 1 --- [nio-8080-exec-3] j.e.resource.webcontainer.jsf.lifecycle  : #{vendaView.enviarReciboPorEmail(registro)}: javax.mail.MessagingException: Exception reading response;
    nested exception is:
      java.net.SocketTimeoutException: Read timed out
  
  // Again, those methods call the method above.
  javax.faces.FacesException: #{vendaView.enviarReciboPorEmail(registro)}: javax.mail.MessagingException: Exception reading response;
    nested exception is:
      java.net.SocketTimeoutException: Read timed out
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118) ~[jsf-impl-2.2.14.jar:2.2.14]
      at javax.faces.component.UICommand.broadcast(UICommand.java:315) ~[jsf-api-2.2.14.jar:2.2]
      at javax.faces.component.UIData.broadcast(UIData.java:1108) ~[jsf-api-2.2.14.jar:2.2]
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) ~[jsf-api-2.2.14.jar:2.2]
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) ~[jsf-api-2.2.14.jar:2.2]
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) ~[jsf-impl-2.2.14.jar:2.2.14]
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) ~[jsf-impl-2.2.14.jar:2.2.14]
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) ~[jsf-impl-2.2.14.jar:2.2.14]
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:658) ~[jsf-api-2.2.14.jar:2.2]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[catalina.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[catalina.jar:9.0.45]
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-websocket.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[catalina.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[catalina.jar:9.0.45]
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78) ~[primefaces-6.0.jar:6.0]
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[catalina.jar:9.0.45]
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[catalina.jar:9.0.45]
  
  </code></pre>
  <p>Can what be done?</p>