Spring Boot 1.2.5.RELEASE - Sending E-mail via Gmail SMTP

23,333

Solution 1

It looks like there's a regression/behaviour change in Java Mail. The change is in both 1.5.3 and 1.5.4. Your app works with Boot 1.2.0 as it uses Java Mail 1.5.2. It fails with Boot 1.2.5 as it uses Java Mail 1.5.4.

The problem in 1.5.3+ appears to be that the SMTP transport connects on port 465 and GMail expects an SSL handshake. Java Mail incorrectly thinks it's not using SSL so it never initiates the handshake and the connection attempt (eventually) times out. You can convince Java Mail to do the right thing by being explicit about the use of SSL. Add the following to application.properties:

spring.mail.properties.mail.smtp.ssl.enable = true

Solution 2

It looks like it is a regression. I have created #3624 to investigate the issue. Thanks for the sample project!

Share:
23,333

Related videos on Youtube

InsFi
Author by

InsFi

Updated on April 05, 2020

Comments

  • InsFi
    InsFi about 4 years

    Firstly, I need to say that sending email with 1.2.0.RELEASE works fine

    application.properties:

    spring.mail.host = smtp.gmail.com
    spring.mail.username = *****@gmail.com
    spring.mail.password = ****
    spring.mail.properties.mail.smtp.auth = true
    spring.mail.properties.mail.smtp.socketFactory.port = 465
    spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
    spring.mail.properties.mail.smtp.socketFactory.fallback = false
    

    pox.xml

    <parent>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.2.0.RELEASE</version>
         <relativePath/>
    </parent>
    

    .......

    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-mail</artifactId>
    </dependency>
    


    After changing parent version to 1.2.5.RELEASE email sending hasn't worked

    Docs says: If spring.mail.host and the relevant libraries (as defined by spring-boot-starter-mail) are available, a default JavaMailSender is created if none exists.


    So i've added

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    

    It hasn't helped and then i've replaced it to

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.4</version>
    </dependency>
    

    Also i've tried

    spring.mail.host = smtp.gmail.com
    spring.mail.username = *****@gmail.com
    spring.mail.password = ****
    spring.mail.port = 465
    

    Result the same.

    It's not a problem to create and configure @Bean manually. But I want to use all beauty of Spring Boot.
    Please point me to my mistakes.

    Thanks in advance

    • dunni
      dunni almost 9 years
      So what is the error message?
    • InsFi
      InsFi almost 9 years
      @dunni, There is not error message. Sender thread simply freezes
    • Stephane Nicoll
      Stephane Nicoll almost 9 years
      So you have a sample project using Spring Boot 1.2.0 and it works. And then you just change to spring boot 1.2.5 and then it breaks? Nothing has changed between those two versions as far as I can see. Can you share the project?
    • InsFi
      InsFi almost 9 years
      @StéphaneNicoll , link I've replaced my credentials with ***. Now it's 1.2.0.RELEASE
  • Juanal
    Juanal almost 8 years
    Worked for me as well. I had an additional error due to my antivirus. Once I disabled mail protection on it, the message was sent successfully
  • Eric Spiegelberg
    Eric Spiegelberg over 7 years
    This answer was very useful. I used it, along with the use of spring-boot's "spring.mail.test-connection=true", to quickly solve my issue.