首页 > 解决方案 > javamail isSSL false 即使我在发送邮件时将 tls 设置为 True

问题描述

我正在尝试使用 javamail 发送电子邮件。它在一个java应用程序中工作。但是当我在我的 Weblogic Web 应用程序中使用它时出现了一些错误。

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTPS");
props.setProperty("mail.smtp.host", "msg.petrochina.com.cn");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.auth", "true");
MailSSLSocketFactory sf = null;
try {
  sf = new MailSSLSocketFactory();
  sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e1) {
  e1.printStackTrace();
}
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.ssl.socketFactory", sf);
final Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("dqlhgysbj@petrochina.com.cn", "123456");
}
};

Session session = Session.getDefaultInstance(props, authenticator);
session.setDebug(true);
MimeMessage mimeMessage = new MimeMessage(session);
  mimeMessage.setFrom(new InternetAddress("dqlhgysbj@petrochina.com.cn","dqlhgys"));
  mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("dqlhgysaccept@petrochina.com"));
  mimeMessage.setSubject("test");
  mimeMessage.setSentDate(new Date());
  mimeMessage.setText("testMailContent","utf-8");
  mimeMessage.saveChanges();
  Transport.send(mimeMessage);

当我运行该应用程序时,我可以在我的日志中看到以下内容

DEBUG: set            
DEBUG: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "msg.petrochina.com.cn", port 465, isSSL false
220 petrochina.com.cn [20137] ESMTP MTA v8.1.2; Mon, 03 Dec 2018 15:59:04 +0800
DEBUG SMTP: connected to host "msg.petrochina.com.cn", port: 465
EHLO SUNWAY-DEV3
250-petrochina.com.cn
250-SIZE 104857600
250-AUTH=LOGIN PLAIN
250-AUTH LOGIN PLAIN
250-STARTTLS
250 8BITMIME
DEBUG SMTP: Found extension "SIZE", arg "104857600"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
STARTTLS
454 tls initialize failed: ssl accept error. (#4.7.0) (eYou MTA)
    javax.mail.MessagingException: 454 tls initialize failed: ssl accept error. (#4.7.0) (eYou MTA)

似乎应用程序正在尝试在没有 ssl 的情况下进行连接,并且无法连接。但我可以使用 telnet 连接地址。远程登录

标签: javaweblogicjakarta-mail

解决方案


首先,修复所有这些常见的 JavaMail 错误

然后,删除mail.transport.protocol设置。(“SMTPS”是错误的协议名称,它应该是“smtps”,但我相信由于上述问题它被忽略了,并且由于您正在设置的属性而您不想要它。)

根据您的邮件服务器,您应该mail.smtp.ssl.enable在第一次连接到服务器时需要使用 SSL,或者您应该需要mail.smtp.starttls.enable使用纯文本连接,然后在连接后切换到 SSL/TLS。你永远不应该两者都需要。

希望这能解决您的问题。

但请注意,您使用的是非常旧的 JavaMail 版本,这一定意味着您使用的是非常旧的 WebLogic 版本。如果可能,请升级到较新版本的 WebLogic。


推荐阅读