Skip to content
Advertisement

Spring Boot email sending throws SocketTimeoutException: Read timed out

I’ve already researched the topic for a few days but none of the answers I found online did the trick for me.

Context: I’ve got a Spring Boot web application which sends automatic emails notifications using Java Mail API and Spring Boot Starter Mail.

It is using GMail SMTP server with a GSuite account. I recently upgraded to use Spring 5.0.6 and Spring Boot 2.0.2 and the email sending stopped working.

A few clues:

  • the Java code sending the email is the same as before
  • Gmail SMTP still works correctly (from another VM using older version of the application with the same settings and authentication, the emails are sent properly).
  • unless I am missing something, the application configuration remains the same as before

The things that have changed:

  • upgrade to Spring 5.0.6
  • upgrade to Spring Boot 2.0.2
  • changes in many places in the Java code to match this upgrades and add features in other parts of the app
  • The IP address of the VM is different than before (AWS EC2 instance)

Here are the relevant dependencies in pom.xml :

JavaScript

Here is the application.yml relevant to Spring mail:

JavaScript

These variables are defined in the environment:

JavaScript

Here is the Spring @Service used to send the email (unchanged):

JavaScript

Now here is the error I get when trying to send an email:

JavaScript

I have tried setting an incorrect SMTP server or bad credentials and this made the connection fail, so I assumed the server and credentials are correct as they are, and the error happens after a successful connection.

The account used hasn’t reached its limit, as another VM uses the same credentials and sends emails without problem.

I’ve tried changing “Start TLS” settings to false and use port 465 instead, but this isn’t working either.

Any help is appreciated !! Thanks in advance!

Advertisement

Answer

After much more trial and error with the configuration, I found out that it required an application property “spring.mail.protocol” in the configuration.

I’ve added the line protocol: smtp in application.yml:

JavaScript

And that fixed the read timeout issue, email are now sent properly. Hope that may help someone in the future.

Advertisement