当前位置: 首页 > news >正文

Java Mail腾讯企业邮箱或其他邮箱发送邮件失败bug记录

问题出现情况

  邮件发送时debug用F8逐步运行可以成功发送邮件,但是用F9或者直接运行程序却发送失败未开启mail的debug模式的报错日志是下面这个:
org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException: [EOF]at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:440)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
  开启了mail的debug日志,然后之前忘了添加超时设置,后续添加了下面超时设置
spring:#JavaMailSendermail:host: smtp.exmail.qq.comport: 587username: xxx@xxx.compassword: xxxdefault-encoding: UTF-8properties:mail.smtp.starttls.enable: truemail.smtp.timeout: 5000 # 设置连接超时(毫秒)后面加的mail.smtp.connectiontimeout: 5000 # 设置连接超时(毫秒)后面加的mail.smtp.writetimeout: 5000 # 设置写入超时(毫秒)后面加的
  之后发现是卡在了权限验证AUTH LOGIN,然后无法进行下一步,直到超时,日志如下:
DEBUG SMTP: exception reading response, THROW: 
java.net.SocketTimeoutException: Read timed outat java.net.SocketInputStream.socketRead0(Native Method)at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)at java.net.SocketInputStream.read(SocketInputStream.java:171)at java.net.SocketInputStream.read(SocketInputStream.java:141)at sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:475)at sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:469)at sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:69)at sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1271)at sun.security.ssl.SSLSocketImpl.access$300(SSLSocketImpl.java:76)at sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:948)at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:126)at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)at java.io.BufferedInputStream.read(BufferedInputStream.java:265)at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:106)at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:2440)at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:1699)at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:759)at javax.mail.Service.connect(Service.java:366)at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)at com.example.demo.mail.MailServiceImpl.send(MailServiceImpl.java:95)at com.example.demo.mail.MailServiceImpl.send(MailServiceImpl.java:108)at com.example.demo.schedule.MyRunner.run(MyRunner.java:20)at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:781)at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:765)at org.springframework.boot.SpringApplication.run(SpringApplication.java:319)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)

问题解决过程

  在debug过程中发现是在TLS握手时超时导致的权限验证失败,在项目启动时添加:
-Djavax.net.debug=ssl:handshake

  添加后查看日志,ServerHello返回的版本是TLSv1.2,但是指定通信的版本是TLSv1.3,成功的日志指定的也是1.3,我尝试配置指定TLS版本为v1.2后发现问题成功解决:

spring:mail:host: smtp.exmail.qq.comport: 587username: xxx@xxx.compassword: xxxdefault-encoding: UTF-8properties:mail.smtp.starttls.enable: truemail.smtp.starttls.required: truemail.smtp.ssl.protocols: TLSv1.2mail.smtp.auth: truemail.smtp.timeout: 5000 # 设置连接超时(毫秒)mail.smtp.connectiontimeout: 5000 # 设置连接超时(毫秒)mail.smtp.writetimeout: 5000 # 设置写入超时(毫秒)

  后续我通过openssl命令行执行查看server的TLS版本发现是支持v1.3的:

admin@DESKTOP-0SP0F5T MINGW64 ~/Desktop
$ openssl s_client -starttls smtp -crlf -connect smtp.exmail.qq.com:587
Connecting to 119.147.6.199
CONNECTED(00000250)
depth=2 C=US, O=DigiCert Inc, OU=www.digicert.com, CN=DigiCert Global Root CA
verify return:1
depth=1 C=US, O=DigiCert Inc, CN=DigiCert Secure Site CN CA G3
verify return:1
depth=0 C=CN, ST=Guangdong Province, L=Shenzhen, O=Tencent Technology (Shenzhen) Company Limited, CN=*.exmail.qq.com
verify return:1
...省略
No client certificate CA names sent
Peer signing digest: SHA256
Peer signature type: RSA-PSS
Server Temp Key: X25519, 253 bits
---
SSL handshake has read 3871 bytes and written 446 bytes
Verification: OK
---
New, TLSv1.3, Cipher is TLS_AES_256_GCM_SHA384
...省略
---
250 8BITMIME
---
Post-Handshake New Session Ticket arrived:
SSL-Session:Protocol  : TLSv1.3Cipher    : TLS_AES_256_GCM_SHA384
...省略

  然而通过ssl-tool在线验证却发现不支持,可自行点击超链接查看
  问了chatgpt发现也是:
在这里插入图片描述
  所以最后解决方法是指定了TLS版本

补充

  项目用的是JDK1.8,SpringBoot版本是2.1.8.RELEASE,后续使用了JDK17,SpringBoot版本3.3.4问题依旧,而且F8调试的不会报错,但是指定了TLS版本的方法虽然成功发送了邮件,但是socket已关闭然后报错。如下:

221 Bye
发送邮件完毕...
2024-10-12 14:04:16.696  INFO 30640 --- [       Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
javax.net.ssl|FINE|01|main|2024-10-12 14:04:16.690 GMT+08:00|SSLSocketImpl.java:497|duplex close of SSLSocket
javax.net.ssl|FINE|01|main|2024-10-12 14:04:16.692 GMT+08:00|SSLSocketImpl.java:1551|close the underlying socket
javax.net.ssl|FINE|01|main|2024-10-12 14:04:16.692 GMT+08:00|SSLSocketImpl.java:1570|close the SSL connection (initiative)
javax.net.ssl|FINE|01|main|2024-10-12 14:04:16.692 GMT+08:00|SSLSocketImpl.java:755|close inbound of SSLSocket
javax.net.ssl|WARNING|01|main|2024-10-12 14:04:16.693 GMT+08:00|SSLSocketImpl.java:515|SSLSocket duplex close failed (
"throwable" : {java.net.SocketException: Socket is closedat java.net.Socket.shutdownInput(Socket.java:1538)at com.sun.mail.util.WriteTimeoutSocket.shutdownInput(WriteTimeoutSocket.java:281)at sun.security.ssl.BaseSSLSocketImpl.shutdownInput(BaseSSLSocketImpl.java:218)at sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:771)at sun.security.ssl.SSLSocketImpl.bruteForceCloseInput(SSLSocketImpl.java:720)at sun.security.ssl.SSLSocketImpl.duplexCloseOutput(SSLSocketImpl.java:580)at sun.security.ssl.SSLSocketImpl.close(SSLSocketImpl.java:504)at com.sun.mail.smtp.SMTPTransport.closeConnection(SMTPTransport.java:1425)at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:1418)at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:476)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)at com.example.demo.mail.MailServiceImpl.send(MailServiceImpl.java:95)at com.example.demo.mail.MailServiceImpl.send(MailServiceImpl.java:105)at com.example.demo.schedule.MyRunner.run(MyRunner.java:20)at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:781)at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:765)at org.springframework.boot.SpringApplication.run(SpringApplication.java:319)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204)

最后

  哪位大佬知道这是为什么吗,最终还是没明白问题出在了哪里,望指点

http://www.lryc.cn/news/458894.html

相关文章:

  • 【大数据】HBase集群断电文件坏块导致集群无法启动处理
  • 400行程序写一个实时操作系统(三):Sparrow的学习方法
  • 为什么人工智能用 Python?
  • 【C++差分数组】P1672何时运输的饲料
  • Go基础知识:切片
  • Redis配置篇 - 指定Redis配置的三种方式,以及Redis配置文件介绍
  • 探索scikit-learn的datasets模块:数据集的加载与使用
  • 手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
  • SSL 协议(HTTPS 协议的关键)
  • test_2_27(C指针)
  • 设计模式——门面模式 | 外观模式
  • FPGA时序分析和约束学习笔记(1、FPGA基本原理)
  • VMware桥接模式无法连接网络
  • YOLO11改进|卷积篇|引入空间通道重组卷积ScConv
  • Java:方法详解
  • Python 三方库下载安装
  • 使用npm i报错node-sass失败问题解决
  • vite+vue3实现动态路径导入
  • JAVA——File类
  • 掌握Postman,开启API测试新纪元!
  • JAVA-数据结构-排序
  • 初识数据结构--时间复杂度 和 空间复杂度
  • Ubuntu QT 交叉编译环境搭建
  • C语言中缓冲区底层实现以及数据输入的处理
  • RocketMQ事务消息原理
  • 【Java】IntelliJ IDEA开发环境安装
  • Go语言中的通道 (Channel) 实践:Goroutine之间的通信
  • 常用类(二)--String类的简单总结
  • Spring Boot开发:从入门到精通
  • 《数据结构》--队列【各种实现,算法推荐】