dos*_*dev 2 email spring smtp yandex
我使用带有下一个 Maven 依赖项的全新 Spring Boot 项目
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
方法的实现
@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();
}
Run Code Online (Sandbox Code Playgroud)
应用程序.yml
host: smtp.yandex.ru
username: some-name@domain.io
password: password
port: 465
Run Code Online (Sandbox Code Playgroud)
并接收下一个异常
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].
Run Code Online (Sandbox Code Playgroud)
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服务完美配合
根据此链接, 我使用了不安全的 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;
Run Code Online (Sandbox Code Playgroud)
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];
Run Code Online (Sandbox Code Playgroud)
我猜 SSL 有问题
类似问题
使用端口: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.
Run Code Online (Sandbox Code Playgroud)
使用 spring boot,向 application.yml 添加属性:
mail:
...
properties:
"mail.smtp.ssl.enable": true
Run Code Online (Sandbox Code Playgroud)
参考:
Outgoing mail
mail server address — smtp.yandex.com
connection security — SSL
port — 465
Run Code Online (Sandbox Code Playgroud)
SMTP 协议提供程序支持以下属性:https : //javaee.github.io/javamail/docs/api/com/sun/mail/smtp/package-summary.html
这个属性对我有帮助
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7806 次 |
| 最近记录: |