Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure SMTP host using YAML file in Spring boot

I am trying to configure an SMTP host for my Spring boot application.

I have this code in my application-dev.yml file:

 mail:
    host: smtp.gmail.com
    port: 465
    username: [email protected]
    password: mypassword

However, when I am trying to send an email I get this exception:

Caused by: javax.mail.MessagingException: Could not connect to 
SMTP host: smtp.gmail.com, 
port: 465, response: -1

Also, I have generated this code with JHipster and I am not sure whether I should modify something else or this is enough to configure a SMTP host.

After changing the port to 587, I am getting this:

org.springframework.mail.MailSendException: 
Failed messages:    com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue 
a STARTTLS command first. jm6sm60863600wjb.27 - gsmtp
like image 339
Elena Alexandra Dobrescu Avatar asked Dec 27 '16 20:12

Elena Alexandra Dobrescu


2 Answers

I made the following changes and now it works fine:

 mail:
    host: smtp.gmail.com
    port: 587
    username: [email protected]
    password: mypassword
    protocol: smtp
    tls: true
    properties.mail.smtp:
        auth: true
        starttls.enable: true
        ssl.trust: smtp.gmail.com
like image 162
Elena Alexandra Dobrescu Avatar answered Nov 08 '22 16:11

Elena Alexandra Dobrescu


I have used following YAML configuration for sending mail via Gmail

spring:
  mail:
    default-encoding: UTF-8
    host: smtp.gmail.com
    username: <Gmail username>
    password: <Gmail password>
    port: 587
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
    protocol: smtp
    test-connection: false
like image 35
Amol Suryawanshi Avatar answered Nov 08 '22 16:11

Amol Suryawanshi