javamail isSSL false 即使我在发送邮件时将 tls 设置为 True

javamail isSSL false even though I've set tls to True when sending mail

我正在尝试使用 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 进行连接,但不会connect.But我可以使用 telnet 连接该地址。​​telnet

首先,修复所有这些common JavaMail mistakes

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

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

希望这能解决您的问题。

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