Java邮件逻辑:无法将套接字转换为TLS

Java邮件逻辑:无法将套接字转换为TLS,java,smtp,jakarta-mail,mailing,Java,Smtp,Jakarta Mail,Mailing,在一个应用程序中,我使用java实现了邮件发送逻辑。我在587端口上使用了smtp.gmail.com和有效的gmail id和密码。在开发环境中,一切正常。但在生产环境中,我需要使用另一个邮件服务器,例如smtp.xyz.inoverport 25,并在该域上使用有效的电子邮件id和密码 当我继续使用SSL时,请使用以下代码启用: 我犯了一个错误 无法将套接字转换为TLS SunCertPathBuilderException:找不到请求目标的有效证书路径 ==================

在一个应用程序中,我使用java实现了邮件发送逻辑。我在
587端口上使用了
smtp.gmail.com
和有效的gmail id和密码。在开发环境中,一切正常。但在生产环境中,我需要使用另一个邮件服务器,例如
smtp.xyz.in
over
port 25
,并在该域上使用有效的电子邮件id和密码

当我继续使用SSL时,请使用以下代码启用:

我犯了一个错误

无法将套接字转换为TLS

SunCertPathBuilderException:找不到请求目标的有效证书路径

=======================================================

final ResourceBundle rsbd=ResourceBundle.getBundle("main/ResourceBundle/Dyna");

        // -- Attaching to default Session, or we could start a new one                                            

                    props.put("mail.smtp.host", smtpServer);

                props.put("mail.smtp.auth", "true");

                props.put("mail.debug", "true");

            props.put("mail.smtp.port", port); 

                props.put("mail.smtp.starttls.enable","true");

            props.put("mail.smtp.EnableSSL.enable","true");

  Session session =Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(admin_mail, admin_password);}});

         // -- Create a new message --
      Message msg = new MimeMessage(session);

     // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress(from));

      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));

      msg.setSubject(subject);
      msg.setText(emailContent);

      // -- Set some other header information --
      msg.setHeader("X-Mailer", "LOTONtechEmail");
      msg.setSentDate(new Date());

        // -- Send the message --
        Transport.send(msg);
props.put("mail.smtp.socketFactory.port","25");

  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

  props.put("mail.smtp.socketFactory.fallback", "true");


  MailSSLSocketFactory sf=new MailSSLSocketFactory();

  sf.setTrustAllHosts(true);

  props.put("mail.smtp.ssl.socketFactory", sf);
当我删除EnableSSL并尝试添加以下代码时:

(获取javax.mail.AuthenticationFailedException:535.7.3身份验证未成功)

==========================================================================

final ResourceBundle rsbd=ResourceBundle.getBundle("main/ResourceBundle/Dyna");

        // -- Attaching to default Session, or we could start a new one                                            

                    props.put("mail.smtp.host", smtpServer);

                props.put("mail.smtp.auth", "true");

                props.put("mail.debug", "true");

            props.put("mail.smtp.port", port); 

                props.put("mail.smtp.starttls.enable","true");

            props.put("mail.smtp.EnableSSL.enable","true");

  Session session =Session.getInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(admin_mail, admin_password);}});

         // -- Create a new message --
      Message msg = new MimeMessage(session);

     // -- Set the FROM and TO fields --
      msg.setFrom(new InternetAddress(from));

      msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email, false));

      msg.setSubject(subject);
      msg.setText(emailContent);

      // -- Set some other header information --
      msg.setHeader("X-Mailer", "LOTONtechEmail");
      msg.setSentDate(new Date());

        // -- Send the message --
        Transport.send(msg);
props.put("mail.smtp.socketFactory.port","25");

  props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

  props.put("mail.smtp.socketFactory.fallback", "true");


  MailSSLSocketFactory sf=new MailSSLSocketFactory();

  sf.setTrustAllHosts(true);

  props.put("mail.smtp.ssl.socketFactory", sf);
通过在过去3天的谷歌搜索,我了解到我需要像given一样配置受信任的证书

但我希望在不加密和不抢劫的情况下继续启用SSL。有没有一种方法可以通过java程序通过我们自己的域发送电子邮件而不启用SSL。
任何帮助都将不胜感激。

是否需要SSL/TLS由您的邮件服务器控制。如果它需要,你必须使用它


您可以将设置为忽略证书问题,也可以按照中所述进行修复。

通常,您只需指定SMTP服务器的主机、端口,以及发送电子邮件时SMTP服务器是否需要身份验证,以及是否应通过SSL/TLS保护与SMTP服务器的通信。您需要正确指定相应的属性,仅此而已

但是,上述参数由SMTP服务器指定。服务器决定是否只接受安全连接;以及它是否应该使用SSL或更安全的TLS,这将是其中之一或两者兼而有之。此外,有些SMTP服务器在发送电子邮件时确实需要发件人身份验证,而有些则不需要。因此,您需要检查要用于正确配置参数的SMTP服务器的文档。如果您正在使用Gmail,请参阅使用Gmail发送电子邮件的工作示例


请删除此代码段以避免SMTP服务器的SSL配置。

嘿,请停用您的防病毒软件,它对我有效


我在antivirus中仅停用SMTP出站

我发现我们还必须设置正确的TLS版本,否则可能会出现相同的错误。下面的属性设置有助于解决此问题

<props>
    <prop key="mail.smtp.auth">true</prop>
    <prop key="mail.smtp.starttls.enable">true</prop>
    <prop key="mail.smtp.ssl.protocols">TLSv1.2</prop>
    <prop key="mail.smtp.ssl.trust">mail.XyZ.com</prop>
</props>

真的
真的
TLSv1.2
mail.XyZ.com

如果使用旧的JavaMail库(mail.jar或javax.mail.jar),可能会发生此错误。
从此处下载最新版本:

错误
javax.mail.AuthenticationFailedException:535 5.7.3身份验证失败
表示您的密码/用户名不正确。此错误与SSL无关。但我可以使用相同的电子邮件和密码访问和发送电子邮件。更多相同的逻辑是在我的工作系统中发送电子邮件,但不在生产服务器中工作(即在某些intranet中的系统上)。您是否尝试使用您的用户名添加域
domain/username
假设域是xyz.in,我使用的是mail.smtp.host:smtp.xyz.in mail.smtp.port:25和邮件地址abc@xyz.in就我所知,一切都是有效的。有没有其他方法可以在不启用SSL的情况下使用自己的域服务器发送邮件?我也有同样的问题,本主题可以帮助您: