【问题标题】:Spring Boot - Java Mail - Couldn't connect to hostSpring Boot - Java Mail - 无法连接到主机
【发布时间】:2021-04-20 18:16:15
【问题描述】:

我已经在我的 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

测试完成

  • 生成应用程序密码
  • 直接使用主机IP
  • 使用端口 465 和 25
  • 禁用 VPS 防火墙
  • 使用了另一个邮件提供商

application.properties

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

【问题讨论】:

  • 您是否尝试过手动设置更高的超时值?
  • @SantiagoWagner 不,所以我测试了添加 spring.mail.properties.mail.smtp.connectiontimeout=5000; spring.mail.properties.mail.smtp.timeout=5000; spring.mail.properties.mail.smtp.writetimeout=5000 - 但永远不会改变

标签: java spring-boot maven email smtp


【解决方案1】:

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

端口 587 在技术上是正确。然而,许多 ESP 已在端口 465 上采用隐式 TLS。 另外,可能有 firewall preventing you from connecting directly,你可能需要connect through a proxy

【讨论】:

  • 我尝试将端口配置为 465,但没有任何变化。 telnet smtp.gmail.com 25 和 telnet smtp.gmail.com 587 工作。我也在禁用防火墙的情况下进行了测试,但结果是一样的。我要研究如何通过代理连接,我测试一下告诉你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
相关资源
最近更新 更多