您现在的位置是:首页 >  云笔记 >  开发随笔 >  文章详情

阿里云服务器发送邮件报错:SMTP host: smtp.163.com, port: 465, response: -1解决方案

特不靠谱   2024-04-24 18:55:45   608人已围观

在本地和其他服务器上运行正常的邮件发送服务,迁移到阿里云上就开始大量报错,信息如下:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com, port: 465, response: -1
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
        at javax.mail.Service.connect(Service.java:367)
        at javax.mail.Service.connect(Service.java:226)
        at javax.mail.Service.connect(Service.java:175)
        at javax.mail.Transport.send0(Transport.java:253)
        at javax.mail.Transport.send(Transport.java:124)

最开始以为是的端口号问题,开放端口号:

图片.png

在服务器上ping 也是正常的:

telnet smtp.163.com 465
Trying 111.124.200.210...
Connected to smtp.163.com.
Escape character is '^]'.

但还是不能发送邮件,代码没动、tomcat没动并且是整体打包迁移到新服务器的,唯一的变量就是重新安装了一个JDK新版本,所以感觉应该是环境问题,安装网上说的方法找到:阿里云的jdk,jdk/jre/lib/securty下的iava.security文件中,jdk.tsl.disabledAlgorithms,去 TLSV1,记得本地和阿里云都要去掉TLSv1

图片.png

改完保存,重启Tomcat,正常了

图片.png

原因:

1、Java环境为例,JRE 高版本把加密算法禁止了 SSLv3, TLSv1, TLSv1.1等,尝试指定版本或删除相关禁用项。而此时你的代码里面刚好又没有指定版本就会出现上面的问题,调整代码也简单:

final Properties props = new Properties();
props.put("mail.smtp.ssl.protocols", "TLSv1.2");


分享到:

编辑发布时间:2024-04-24 19:29:23