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

标签 java weblogic jakarta-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 的情况下进行连接,但无法连接。但我可以使用 telnet 连接该地址。 telnet

最佳答案

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

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

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

希望这能解决您的问题。

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

关于javamail isSSL false 即使我在发送邮件时将 tls 设置为 True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53590741/

相关文章:

Hibernate 持久类上的 java.lang.NullPointerException

java - 从字符串打印偶数字符 - 错误非静态方法无法从静态上下文引用

java - 当 hashmap 值是多个属性 java 8 的对象列表时如何排序

java - System.currentTimeMillis() 转 int 返回负值

java - 获取 javax.mail.NoSuchProviderException : imap when trying to poll a mail server

java EE 安装和设置

java - weblogic 12.2.1.1.0 中的 "WELD-001409 Ambiguous dependencies for type "

transactions - Weblogic 事务超时 : how to set in admin console in WebLogic AS 8. 1

jakarta-ee - 我在哪里可以找到 JavaEE 包的来源?

JavaMail 通过 vps 发送电子邮件,不需要身份验证,但得到 AuthenticationFailedException - 为什么?