Spring Boot使用GMX发送邮件遇TLS握手错误求助
我尝试使用Spring Boot发送邮件,选用GMX作为邮件服务商,但控制台出现以下错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.mail.MailSendException: Mail server connection failed. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate); message exceptions (1) are:
Failed message 1: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)] with root causejavax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol
is disabled or cipher suites are inappropriate)
核心代码:
@Autowired private JavaMailSender javaMailSender; private void sendMail() { SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo("to_1@gmail.com", "to_2@gmail.com", "to_3@yahoo.com"); msg.setSubject("Testing from Spring Boot"); msg.setText("Hello World \n Spring Boot Email"); javaMailSender.send(msg); }
pom.xml配置:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.8.RELEASE</version> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.7</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
application.properties配置:
spring.mail.protocol=smtp spring.mail.host=mail.gmx.net spring.mail.properties.mail.smtp.ssl.trust=mail.gmx.net spring.mail.port=587 spring.mail.username=XYZ@gmx.de spring.mail.password=xyz # Other properties spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000 # TLS , port 587 spring.mail.properties.mail.smtp.starttls.enable=true
问题原因与解决步骤
核心原因
错误根源是:JVM默认禁用了GMX兼容的旧版TLS协议(如TLSv1.2),同时pom.xml中存在邮件依赖版本冲突,导致协议协商失败。
解决步骤
- 清理冗余依赖
spring-boot-starter-mail已经封装了邮件发送所需的所有依赖,不需要单独引入javax.mail和spring-context-support。修改pom.xml,只保留核心依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
- 强制指定TLS协议版本
在application.properties中添加配置,明确指定GMX支持的TLSv1.2协议,同时强制开启STARTTLS:
spring.mail.properties.mail.smtp.ssl.protocols=TLSv1.2 spring.mail.properties.mail.smtp.starttls.required=true
- 验证GMX账户配置
- 确认GMX账户已开启SMTP服务(GMX默认开启,但需检查账户设置)
- 若开启了两步验证,必须使用应用专用密码替代原登录密码,否则会验证失败
- JVM安全配置调整(可选)
如果上述配置仍无效,需修改JRE目录下的lib/security/java.security文件:
- 找到
jdk.tls.disabledAlgorithms配置项,移除其中的TLSv1.2(若存在) - 重启应用生效
最终完整配置示例
修改后的application.properties:
spring.mail.protocol=smtp spring.mail.host=mail.gmx.net spring.mail.port=587 spring.mail.username=XYZ@gmx.de spring.mail.password=你的应用专用密码(开启两步验证时使用) spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=5000 spring.mail.properties.mail.smtp.writetimeout=5000 spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.trust=mail.gmx.net spring.mail.properties.mail.smtp.ssl.protocols=TLSv1.2
内容的提问来源于stack exchange,提问作者Captai-N

