Spring Boot - 无法连接到 SMTP 主机:smtp.gmail.com,端口:25,响应:421

Posted

技术标签:

【中文标题】Spring Boot - 无法连接到 SMTP 主机:smtp.gmail.com,端口:25,响应:421【英文标题】:Spring Boot - Could not connect to SMTP host: smtp.gmail.com, port: 25, response: 421 【发布时间】:2015-03-19 20:03:38 【问题描述】:

我正在使用 gmail smtp 主机通过 spring boot 和 JavaMail Sender 发送邮件:

我的邮件属性:

 spring.mail.host = smtp.gmail.com
 spring.mail.username = XXX@gmail.com
 spring.mail.password = XXX

 spring.mail.properties.mail.smtp.auth = true
 spring.mail.properties.mail.smtp.socketFactory.port = 465
 spring.mail.properties.mail.smtp.starttls.enable = true
 spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
 spring.mail.properties.mail.smtp.socketFactory.fallback = false

出现错误:

Failed message 1: javax.mail.MessagingException: Could not connect to SMTP host: smtp.9business.fr, port: 25, response: 421] with root cause

即使我使用 465 端口,他为什么指向 25 端口?

【问题讨论】:

添加spring.mail.port=465 更改端口。如果这不起作用,请添加您得到的错误。 【参考方案1】:

我不确定你从哪里得到这些属性。可以在这里找到更常见的 Spring Boot 属性配置:

http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

所以你可能应该使用spring.mail.portspring.mail 命名空间中可用的属性有:

host
port
username
password
defaultEncoding (default: "UTF-8")

但是,如果您要创建自己的JavaMailSender,则设置 SMTP 端口的属性是 mail.smtp.port。我将JavaMailSender 设置为像这样的bean:

@Value(value = "$mail.smtp.host")
private String smtpHost;

@Value(value = "$mail.smtp.port")
private String smtpPort;

@Bean
public JavaMailSender mailSender() 
    JavaMailSenderImpl sender = new JavaMailSenderImpl();

    Properties p = new Properties();
    p.setProperty("mail.smtp.auth", "false");
    p.setProperty("mail.smtp.host", smtpHost);
    p.setProperty("mail.smtp.port", smtpPort);
    sender.setJavaMailProperties(p);

    return sender;

【讨论】:

我尝试将属性 spring.mail.port = 587 添加到我的文件属性中,现在它指向正确的端口,但仍然出现相同的错误,但使用正确的端口失败消息 1 : javax.mail.MessagingException: 无法连接到 SMTP 主机:smtp.gmail.com,端口:587; @Steve Spring Boot 支持自动配置JavaMailSenderImpl,只需指定application.properties 中的属性即可。请参阅参考指南的mail 部分和properties section。 这就是我提到它的原因,但为了清楚起见,我添加了邮件配置类的完整列表。我已经习惯了自己创建发件人,因为我需要禁用身份验证才能在当前环境中连接到 MS Exchange。由于某种原因,Spring Boot 自动邮件发件人似乎不支持设置 mail.smtp.auth 属性。【参考方案2】:

实际上我发现了问题所在,我应该使用其中一个是我服务器的端口,另一个是 gmail 服务器的端口:

spring.mail.properties.mail.smtp.socketFactory.port = 25
mail.smtp.port= 465

【讨论】:

我对Spring Boot一无所知,但一般你应该never need to set the socket factory properties。另请参阅general JavaMail instructions for using Gmail。【参考方案3】:

在您的属性文件中将 ma​​il.smtp.starttls.required 禁用为 false

spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=false

【讨论】:

感谢我添加了“spring.mail.properties.mail.smtp.starttls.enable”,它对我来说很好用。顺便说一句,我在docs.spring.io/spring-boot/docs/current/reference/html/… 中找不到这个属性。有什么想法吗? spring.mail.properties 是在 spring-boot-autoconfigure 中添加的前缀,但是“mail.smtp.starttls.enable”是 Java 邮件 API 中的原始属性名称。 Chill.. 感谢@Ajay 提供的信息。 为了方便,这里可以看到“mail.smtp.*”java属性:javaee.github.io/javamail/docs/api/com/sun/mail/smtp/…【参考方案4】:

试试这个

spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.username = xxxxxx
spring.mail.password = xxxxxx
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.starttls.required = true
spring.mail.properties.mail.smtp.auth = true

确保 google 允许安全性较低的应用: https://myaccount.google.com/lesssecureapps 打开它

【讨论】:

以上是关于Spring Boot - 无法连接到 SMTP 主机:smtp.gmail.com,端口:25,响应:421的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot + MySQL + Docker Compose - 无法使 Spring Boot 连接到 MySQL

无法将mysql连接到spring boot项目

无法将 spring-boot 2 服务连接到不同容器中的 mysql

Docker 上的 Spring Boot 无法连接到 MySQL

Docker:无法连接到弹性搜索表单spring boot docker image

无法使用spring boot连接到mysql数据库