10

I use brand new spring boot project with next Maven dependency

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

Implementation of method

 @Autowired
JavaMailSender emailSender;

@GetMapping("/send")
public String send() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("[email protected]");
    message.setTo("[email protected]");
    message.setSubject(null);
    message.setText("Hello World");
    emailSender.send(message);
    return "success send email " + now();
}

application.yml

host: smtp.yandex.ru
username: [email protected]
password: password
port: 465

And receive the next exception

2020-08-03 23:02:35.102 ERROR 21615 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Got bad greeting from SMTP host: smtp.yandex.com, port: 465, response: [EOF]. 

Failed messages: javax.mail.MessagingException: Got bad greeting from SMTP host: smtp.yandex.com, port: 465, response: [EOF]; message exceptions (1) are: Failed message 1: javax.mail.MessagingException: Got bad greeting from SMTP host: smtp.yandex.com, port: 465, response: [EOF]] with root cause

But the same code works perfectly with Mailtrap service

According to this link I used not secure 25 port After which I received the next exception

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.yandex.ru, 25; timeout -1;

587 port =

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: [EOF];

I guess a problem with SSL

Similar issue

dos4dev
  • 429
  • 2
  • 10
  • 26

2 Answers2

19

With port: 465 (SMTP Protocol), you can try enable ssl via properties:

"mail.smtp.ssl.enable": true

mail.smtp.ssl.enable: If set to true, use SSL to connect and use the SSL port by default. Defaults to false for the "smtp" protocol and true for the "smtps" protocol.

Using spring boot, add properties to application.yml:

 mail:
   ...
   properties:
     "mail.smtp.ssl.enable": true

References:

Outgoing mail
mail server address — smtp.yandex.com
connection security — SSL
port — 465

The SMTP protocol provider supports the following properties: https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html

huytmb
  • 3,647
  • 3
  • 12
  • 18
4

This properties help me

spring:
 boot:
   admin:
     mail:
       to: ${spring.mail.username}, [email protected]
       from: ${spring.mail.username} 
 mail:
   host: smtp.yandex.ru
   username: [email protected]
   password: password
   port: 587
   protocol: smtp
   properties:
     "mail.transport.protocol": smtp
     "mail.smtp.auth": true
     "mail.smtp.starttls.enable": true
dos4dev
  • 429
  • 2
  • 10
  • 26