Spring Boot-Java Mail-无法连接到主机 [英] Spring Boot - Java Mail - Couldn't connect to host
问题描述
我已经使用JavaMailSender在我的Java应用程序中实现了电子邮件管理器,并配置了Gmail电子邮件并进行了本地测试.
I've implemented the email manager using JavaMailSender into my Java application, configured with Gmail email, and locally tested.
我正在使用freenom的免费域.
I'm using a free domain by freenom.
一切正常.但是在服务器上部署应用程序会遇到此错误:
in localhost on my pc everything working perfectly. But deploying on the server the application obtains this error:
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
推荐答案
尝试将spring.mail.port更改为465.这可能会有所帮助.
Try to change spring.mail.port to 465. This might help.
端口 587 在技术上正确.但是,许多ESP 已在端口465 上采用了隐式TLS .另外,可能有一个防火墙阻止您直接连接,您可能需要
Port 587 is technically correct. However, many ESPs have adopted implicit TLS on port 465. Also, there's probably a firewall preventing you from connecting directly, you may need to connect through a proxy.
这篇关于Spring Boot-Java Mail-无法连接到主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!