收到来自 SMTP 主机的错误问候:smtp.yandex.ru,端口:465,响应:[EOF]] 根本原因是 Yandex

Got bad greeting from SMTP host: smtp.yandex.ru, port: 465, response: [EOF]] with root cause Yandex

我使用全新的 spring 引导项目和下一个 Maven 依赖项

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

方法的实现

 @Autowired
JavaMailSender emailSender;

@GetMapping("/send")
public String send() {
    SimpleMailMessage message = new SimpleMailMessage();
    message.setFrom("some-name@domain.com");
    message.setTo("toReceiver@gmail.com");
    message.setSubject(null);
    message.setText("Hello World");
    emailSender.send(message);
    return "success send email " + now();
}

application.yml

host: smtp.yandex.ru
username: some-name@domain.io
password: password
port: 465

并收到下一个异常

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

但相同的代码与 Mailtrap 服务完美配合

根据这个 link 我使用了不安全的 25 端口 之后我收到下一个异常

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端口=

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];

我猜 SSL 有问题

相似issue

这个属性对我有帮助

spring:
 boot:
   admin:
     mail:
       to: ${spring.mail.username}, test@yandex.ru
       from: ${spring.mail.username} 
 mail:
   host: smtp.yandex.ru
   username: test@yandex.ru
   password: password
   port: 587
   protocol: smtp
   properties:
     "mail.transport.protocol": smtp
     "mail.smtp.auth": true
     "mail.smtp.starttls.enable": true

使用端口:465(SMTP 协议),您可以尝试通过属性启用 ssl:

"mail.smtp.ssl.enable": 真

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.

使用spring引导,将属性添加到application.yml:

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

参考文献:

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

SMTP 协议提供程序支持以下属性: https://javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html