使用SpringBoot + JavaMailSender 发送邮件报错 Mail server connection failed;Could not connect to SMTP host

报错信息如下:

java

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 连接

yml

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 文件,应该修改为:

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 文件在线转换的网站 在线yaml转properties-在线properties转yaml-ToYaml.com

 

来源:https://www.cnblogs.com/songjilong/p/12577010.html

版权声明:本文为博主作者:zhamors原创文章,版权归属原作者,如果侵权,请联系我们删除!

原文链接:https://blog.csdn.net/qq_39128430/article/details/133099368

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
社会演员多的头像社会演员多普通用户
上一篇 2024年1月8日
下一篇 2024年1月8日

相关推荐