本地代码无问题邮件却发送失败的问题 您所在的位置:网站首页 内网邮箱发送失败原因 本地代码无问题邮件却发送失败的问题

本地代码无问题邮件却发送失败的问题

2024-07-17 02:41| 来源: 网络整理| 查看: 265

项目场景:

通过hutool写一段邮件发送代码,发现总是发送失败,并且报错:

问题描述:

当时我的代码是这样写的:

public static void main(String[] args) { MailSenderVo mailSenderVo = new MailSenderVo(); mailSenderVo.setHost("smtp.exmail.qq.com"); mailSenderVo.setPort(465); mailSenderVo.setAuth(true); mailSenderVo.setFrom("开启了smtp的代理邮箱"); mailSenderVo.setUser("开启了smtp的代理邮箱"); mailSenderVo.setPass("代理邮箱开启SMTP的时候获取的密码"); mailSenderVo.setSslEnable(true); String subject = "邮件标题"; String content = "邮件正文内容"; try { log.info("邮件开始发送!"); MailUtil.send(mailSenderVo, "要发送的目标邮箱地址", subject, content, false); log.info("邮件发送成功!"); } catch (Exception e) { log.error(e.getMessage()); } }

报的错打印出来是这样的: MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465

原因分析:

一开始怀疑是我的SSL配置没有改成true,百度了很多解决方案也都是说报这个错是因为这个ssl协议没有配置成true导致465访问不通,但是我的确配置的是true啊!!!!!!!真让人头大! 但是!突然定睛一看,我发现我把错误try catch住了,打印出来的错误未必是真正的错误,或许是这个错误的父类。

解决方案: public static void main(String[] args) { MailSenderVo mailSenderVo = new MailSenderVo(); mailSenderVo.setHost("smtp.exmail.qq.com"); mailSenderVo.setPort(465); mailSenderVo.setAuth(true); mailSenderVo.setFrom("开启了smtp的代理邮箱"); mailSenderVo.setUser("开启了smtp的代理邮箱"); mailSenderVo.setPass("代理邮箱开启SMTP的时候获取的密码"); mailSenderVo.setSslEnable(true); String subject = "邮件标题"; String content = "邮件正文内容"; // try { log.info("邮件开始发送!"); MailUtil.send(mailSenderVo, "要发送的目标邮箱地址", subject, content, false); log.info("邮件发送成功!"); // } catch (Exception e) { // log.error(e.getMessage()); // } }

我把try catch去掉后,用main方法直接运行这段代码,真正的报错出现了!

- 邮件开始发送! Exception in thread "main" cn.hutool.extra.mail.MailException: MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465 at cn.hutool.extra.mail.Mail.send(Mail.java:359) at cn.hutool.extra.mail.MailUtil.send(MailUtil.java:395) at cn.hutool.extra.mail.MailUtil.send(MailUtil.java:191) at cn.hutool.extra.mail.MailUtil.send(MailUtil.java:173) at cn.hutool.extra.mail.MailUtil.send(MailUtil.java:158) at cn.net.cfss.fgbp.face.unifiedapi.service.collectionApplets.Collection10015Service.main(Collection10015Service.java:128) Caused by: javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465; nested exception is: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at cn.hutool.extra.mail.Mail.doSend(Mail.java:373) at cn.hutool.extra.mail.Mail.send(Mail.java:357) ... 5 more Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate) at sun.security.ssl.HandshakeContext.(HandshakeContext.java:171) at sun.security.ssl.ClientHandshakeContext.(ClientHandshakeContext.java:101) at sun.security.ssl.TransportContext.kickstart(TransportContext.java:238) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:394) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:354) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:211) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927) ... 13 more Process finished with exit code 1

原来是报错:javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)导致最后报错:MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465,难怪我一直找不到原因!

然后百度一下这个错误,发现是我本地环境的jdk1.8的配置中禁止了这个SLLv3协议,所以本地发送邮件才会一直报错。

真正的解决方案是: 打开jdk的配置的文件:C:\Program Files\Java\jdk1.8.0_291\jre\lib\security\java.security,找到SSLv3,然后删掉,不让本地JDK禁止这个协议就能发送了。

但是!!!!!!!!!!!!!!!!! 万万没想到啊! 去掉了还是不行,这个时候我陷入了沉思… 然后我一百度发现, 诶,原来sslv3后面还有两个协议是和sslv3是类似的协议TLSv1, TLSv1.1,需要一起删除的!!!!!!! 然后我删除后重启了项目,本地发送邮件果然没问题了~ 嗨呀!



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有