You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu下Java邮件发送遇SSLHandshakeException问题求助

Ubuntu环境下Java邮件发送SSL握手异常问题排查

我在Ubuntu系统上运行的Java应用中,使用jakarta.mail-api 2.1.2版本和OpenJDK 11.0.20.1版本发送邮件时,抛出如下异常:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)

我的代码实现如下:

Properties props = new Properties();
props.put("mail.smtp.host", "smtp.office365.com"); //SMTP服务器
props.put("mail.smtp.port", "587"); //TLS端口
props.put("mail.smtp.auth", "true"); //启用身份验证
props.put("mail.smtp.starttls.enable", "true"); //启用STARTTLS
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.debug", "true");

Authenticator auth = new Authenticator() {
   protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication("abc@xxxx.com", "yyyy");
   }
};

Session session = Session.getInstance(props, auth);

try {
   MimeMessage msg = new MimeMessage(session);
   msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
   msg.addHeader("format", "flowed");
   msg.addHeader("Content-Transfer-Encoding", "8bit");

   msg.setFrom(new InternetAddress("abc@xxxx.com"));
   msg.setReplyTo(InternetAddress.parse("abc@xxxx.com", false));
   msg.setSubject("邮件主题");
   msg.setText("邮件内容", "UTF-8");
   msg.setRecipients(Message.RecipientType.TO,   InternetAddress.parse("me@xxxx.com", false));
     
   Transport.send(msg);  
   
   log.info("邮件发送成功!");
} catch (Exception e) {
   log.error("发送错误",e);
}

相同代码在安装了Java 11.0.19版本的Windows 11机器上可正常运行,求解决建议?


更新排查进展

按照建议,我将代码中的协议配置行修改:
原代码:

props.put("mail.smtp.ssl.protocols", "TLSv1.2");

修改为:

props.put("mail.smtp.ssl.protocols", "TLSv1.3");

但异常依旧存在。

查看Java配置文件java.security,发现其中的禁用算法配置:

jdk.tls.disabledAlgorithms=SSLv3, TLSv1, TLSv1.1, RC4, DES, 
MD5withRSA, \
DH keySize < 1024, EC keySize < 224, 3DES_EDE_CBC, anon, NULL, \
include jdk.disabled.namedCurves
  • 注释上述三行配置后,抛出新异常:

javax.mail.AuthenticationFailedException: null
at javax.mail.Service.connect(Service.java:306) ~[mail-1.4.jar:1.4]

  • 仅移除配置中的TLSv1、TLSv1.1项后,仍会抛出最初的SSL握手异常:

javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)


内容的提问来源于stack exchange,提问作者acco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 05:23:11