关闭

java发送邮件报错Mail server connection failed;

瞎溜达 1年前 ⋅ 311 阅读

阿里云ECS服务器发送邮件时报:

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 

 ECS 服务器上,默认禁用了 25 端口,所以在通过 25 端口去连接邮件服务器时,无法连上,就报超时了。官方建议使用 465 端口,而 456 端口是 SSL 协议的,所以不仅要换端口,还需要进行 SSL 协议替换。

Spring mail配置

spring:
    mail:
        host: smtp.163.com
        username: ****** #邮箱地址
        password: ****** #邮箱授权密码,邮箱设置里获取
        port: 465
        default-encoding: utf-8
        protocol: smtp
        properties:
            mail:
                smtp:
                    auth: true
                    starttls:
                        enable: true
                        required: true
                    ssl:
                        enable: true
                        socketFactory:
                            port: 465
                            class: javax.net.ssl.SSLSocketFactory

全部评论: 0

    我有话说: