首页 > 解决方案 > Spring Boot - Java Mail - 无法连接到主机

问题描述

我已经在我的 Java 应用程序中使用 JavaMailSender 实现了电子邮件管理器,配置了 Gmail 电子邮件,并进行了本地测试。

我正在使用freenom的免费域。

在我的电脑上的 localhost 中,一切正常。但是在服务器上部署应用程序会得到这个错误:

c.m.a.m.components.EmailManager          : sendRegisterEmail Exception: Mail server connection failed; 
nested exception is com.sun.mail.util.MailConnectException: 
Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
        java.net.UnknownHostException: smtp.gmail.com. Failed messages: 
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
  nested exception is:
        java.net.UnknownHostException: smtp.gmail.com

测试完成

应用程序属性

spring.mail.host=smtp.gmail.com
spring.mail.port=587

spring.mail.username=***********@gmail.com
spring.mail.password=***********

spring.mail.properties.mail.debug=false

spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.mime.charset=UTF-8
spring.mail.transport.protocol=smtp

标签: javaspring-bootmavenemailsmtp

解决方案


尝试将 spring.mail.port 更改为 465。这可能会有所帮助。

587端口在技术上是正确的。但是,许多 ESP在端口 465 上采用了隐式 TLS 。此外,可能有防火墙阻止您直接连接,您可能需要通过代理连接


推荐阅读