java - 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
测试完成
- 生成应用程序密码
- 直接使用主机IP
- 使用端口 465 和 25
- 禁用 VPS 防火墙
- 使用了另一个邮件提供商
应用程序属性
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
解决方案
尝试将 spring.mail.port 更改为 465。这可能会有所帮助。
587端口在技术上是正确的。但是,许多 ESP在端口 465 上采用了隐式 TLS 。此外,可能有防火墙阻止您直接连接,您可能需要通过代理连接。
推荐阅读
- c# - .NET Core 表单不显示 URL
- java - Servlet 过滤器无法正常工作
- mysql - 如何按 JSON 值排序?
- bash - entrypoint.sh - line 9: syntax error: unexpected "("
- forms - Redirect to page after successfully entering in information in Squarespace
- json - How to extract lat lng from JSON using flutter?
- javascript - 使用 React useState 访问嵌套数组数据时遇到问题
- c++ - 为什么递归函数以预期的方式更改其参数(C++)?
- python - 在线使用Airtable Python Wrapper文档问题
- java - MySQL trigger working through direct insert, but not when committing through Hibernate