使用 SpringBoot 发送邮件时,出现 Mail server connection failed 异常问题

背景

按照之前写的文章 SpringBoot 中实现邮件发送功能 ,实现邮件发送功能后,在本地开发环境测试,邮件能够正常发送。但部署到阿里云 ECS 服务器以后,一直没有收到邮件。

一开始怀疑是条件判断有问题,后来添加 log 后发现,条件状态判断正常。再查看打印的相关 log,发现是在邮件发送时出现了异常。

问题报错

使用 SpringBoot 的邮件发送组件功能,发送邮件时,本地能够发送成功,但部署到阿里云 ECS 服务器后,却发送失败,部分关键 log 信息如下,

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: 连接超时 (Connection timed out). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.163.com, 25; timeout -1;
  nested exception is:
        java.net.ConnectException: 连接超时 (Connection timed out)
        at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:447)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:322)
        at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:311)

问题解决

从现有情况看,跟程序运行环境有关。查看相关资料,发现在阿里云 ECS 服务器上,默认禁用了 25 端口,所以在通过 25 端口去连接邮件服务器时,无法连上,就报超时了。

官方建议使用 465 端口,而 456 端口是 SSL 协议的,所以不仅要换端口,还需要进行 SSL 协议替换。下面是在 application.properties 进行的邮件发送相关配置,

# JavaMailSender Config
spring.mail.host=smtp.163.com
spring.mail.username=xxx@163.com #你的163邮箱地址
spring.mail.password=xxx #你的163邮箱授权密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

# SSL Config
spring.mail.port=465
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

经过上面这样配置后,在阿里 ECS 上就能够正常发送邮件了。

服务器端口协议

163 邮箱相关服务器信息如下,

服务器名称服务器地址SSL 协议端口非 SSL 协议端口
SMTPsmtp.163.com465/99425

欢迎关注我的公众号,一起进步!
在这里插入图片描述

  • 23
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
引用\[1\]和\[2\]中提到的错误"CondaHTTPError: HTTP 000 CONNECTION FAILED"通常是由于网络连接问题导致的。这个错误可能是由于Anaconda服务器无法访问或被阻止访问所致。解决这个问题的方法有几种。 首先,你可以检查你的网络连接是否正常。确保你的网络连接稳定,并且没有任何防火墙或代理服务器阻止了对Anaconda服务器的访问。 如果你确定网络连接没有问题,你可以尝试使用清华镜像源来替代Anaconda默认的源。你可以使用以下命令将清华镜像源添加到你的conda配置中: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --set show_channel_urls yes 这将把清华镜像源添加到你的conda配置中,并且允许你从清华镜像源下载和安装软件包。 如果以上方法都没有解决问题,你可以尝试使用conda的--proxy选项来设置代理服务器。你可以使用以下命令来设置代理服务器: conda config --set proxy_servers.http http://your_proxy_server:port conda config --set proxy_servers.https https://your_proxy_server:port 将"your_proxy_server"替换为你的代理服务器的地址,"port"替换为代理服务器的端口号。 通过以上方法,你应该能够解决CondaHTTPError: HTTP 000 CONNECTION FAILED错误,并成功使用conda进行操作。 #### 引用[.reference_title] - *1* [Anaconda入门:解决CondaHTTPError: HTTP 000 CONNECTION FAILED for url](https://blog.csdn.net/guotianqing/article/details/108650253)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Conda使用CondaHTTPError: HTTP 000 CONNECTION FAILED错误解决](https://blog.csdn.net/qq_24655701/article/details/123442980)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [anaconda在创建虚拟环境出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url问题](https://blog.csdn.net/lyydcg/article/details/115967216)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToSimpleL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值