使用SpringBoot + JavaMailSender 发送邮件报错 Mail server connection failed;Could not connect to SMTP host
2024-10-08 22:33:22
说明:
出于安全考虑,阿里云默认封禁 TCP 25 端口出方向的访问流量,无法在阿里云上的云服务器通过 TCP 25 端口连接外部地址。
【官方提示】:如果您需要使用阿里云上的云服务器对外部发送邮件,建议您使用阿里云邮产品或者使用第三方邮件服务商提供的 465 端口。记得添加安全组配置。
如果你确实需要使用 25 端口,可在安全管控平台中提交25端口解封申请,地址:https://help.aliyun.com/knowledge_detail/56130.html
但修改为 465 端口后发送邮件会报错
报错信息如下:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
解决办法:
在 application.yml 中将邮件配置为 SSL 连接
spring:
mail:
host: smtp.qq.com
username: 你的邮箱账号
password: 你的邮箱授权码(QQ邮箱)
port: 465
protocol: smtp
default-encoding: utf-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
ssl:
enable: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
如果你使用的是 properties 文件,应该修改为:
# JavaMailSender 配置
spring.mail.host=smtp.qq.com
spring.mail.username=你的邮箱账号
spring.mail.password=你的邮箱授权码(QQ邮箱)
spring.mail.port=465
spring.mail.default-encoding=utf-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# SSL 配置
spring.mail.protocol=smtp
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
推荐一个 yml、properties 文件在线转换的网站 https://toyaml.com/index.html
最新文章
- Windows进程间通信—命名管道
- iOS 面试题搜集
- 作业七:团队项目——Alpha版本冲刺阶段005
- Android开发小问题记录
- hdu 4010 动态树 @
- 【转】PowerShell入门(五):Cmd命令与PowerShell命令的交互
- python多线程threading
- cocos2d-x知识巩固-基础篇(1)
- mysqldump的常用语句及各参数详解
- ES聚合实例
- 深度学习:Keras入门(二)之卷积神经网络(CNN)
- Srping - bean的依赖注入(Dependency injection)
- 爬虫——xpath
- Struts1 不能进入Action,页面空白问题解决
- Mysql优化系列--Innodb引擎下mysql自身配置优化-转
- EasyUI tree reload时更改参数的问题。
- PreviewRenderUtility的Example
- C#学习笔记(27)——委托排序(1)
- FMX StringGrid向上滑动自动加载记录(二)
- 九度 1500:出操队形(LIS变形)
热门文章
- Vue错误信息解决
- 831. KMP字符串
- 恭祝2018元旦快乐!转发享福利(⊙o⊙)…
- 烘焙ID贴图
- VMWare vCenter 4.x 数据库由SQL Express 迁移至SQL Server 2008 R2
- JavaScript中如何给按钮设置隐藏与显示属性
- js操作复选框
- KEMET新型电容器推动了电动汽车技术的发展
- SpringBoot图文教程11—从此不写mapper文件「SpringBoot集成MybatisPlus」
- 7-1 jmu-python-汇率兑换 (10 分)