我正在尝试使用 jakarta mail、tls 和 checkserveridentity 发送电子邮件,但失败并出现异常:
jakarta.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.io.IOException: Can't verify identity of server: XXXXXXXXXXX.com at [email protected]/org.eclipse.angus.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2173)
当我深入挖掘时,它发生在 org.eclipse.angus.mail.util.SocketFetcher 类中:
java.lang.IllegalAccessException: class org.eclipse.angus.mail.util.SocketFetcher (in module org.eclipse.angus.mail) cannot access class sun.security.util.HostnameChecker (in module java.base) because module java.base does not export sun.security.util to module org.eclipse.angus.mail
我尝试使用 Java jdk 11 和 17 我正在使用 jakarta.mail-api-2.1.2、jakarta.activation-api-2.1.2、angus-mail-2.0.2、angus-activation-2.0.1
我发现了一个看起来相似但没有帮助的错误报告: 文字
我的代码是:
String to = "[email protected]";
String from = "[email protected]";
String host = "mail.host.com";
final String username = "[email protected]";
final String password = "somePWD";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.ssl.checkserveridentity", "true");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.debug", "true");
Session session = Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication
getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
// header field of the header.
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("subject");
message.setText("Test mail");
// Send message
Transport.send(message, username, password);
}
catch (MessagingException mex) {
mex.printStackTrace();
}
没有 checkserveridentity 它就可以正常工作(properties.setProperty("mail.smtp.ssl.checkserveridentity", "false")。
主机使用位于我的默认信任库中的 Let's Enrcypt 证书,因此这不应该是问题。
有人有任何建议、解决方法、错误修复、新版本吗?
您找到了正确的链接,该链接具有使用 --add-opens 的正确解决方法,但您必须查看异常中的模块名称以查看是否添加了正确的打开。发生的情况是,在编写注释后,JakartaMail 将 API 从实现中分离出来,您需要将模块名称从
jakarta.mail
更改为 org.eclipse.angus.mail
--add-opens 'java.base/sun.security.util=org.eclipse.angus.mail'
此问题 Angus Mail:com.sun.mail.util.SocketFetcher #124 的非法反射访问应在 Angus Mail 2.0.3 中修复,并附带一些 警告。