javax.mail.MessaginException:Can';t向SMTP主机发送命令

javax.mail.MessaginException:Can';t向SMTP主机发送命令,java,websphere,jakarta-mail,Java,Websphere,Jakarta Mail,尝试从Java mail API发送邮件时,我遇到以下异常: javax.mail.MessaginException:无法向SMTP主机发送命令; 嵌套异常为:javax.net.ssl.SSLHandshakeException:com.ibm.jsse2.util.j:PKIX路径生成失败:java.security.cert.CertPathBuilderException:PKIXCertPathBuilderImpl无法生成有效的CertPath。;内因是: java.securit

尝试从Java mail API发送邮件时,我遇到以下异常:
javax.mail.MessaginException:无法向SMTP主机发送命令;
嵌套异常为:javax.net.ssl.SSLHandshakeException:com.ibm.jsse2.util.j:PKIX路径生成失败:java.security.cert.CertPathBuilderException:PKIXCertPathBuilderImpl无法生成有效的CertPath。;内因是:
java.security.cert.CertPathValidator异常:CN=Baltimore CyberTrust Root、OU=CyberTrust、O=Baltimore、C=IE颁发的证书不受信任;内因是:
java.security.cert.CertPathValidator异常:证书链接错误

下面是我的Java代码。同样的代码在我的本地系统上运行良好。当我在tomcat服务器上发布web应用程序,但相同的代码不起作用时

它显示了上述例外情况

public static void sendMail(String toMailIds,String subject,String htmlMsg){
        String[] recipientList = toMailIds.split(",");
        InternetAddress[] recipientAddress = new InternetAddress[recipientList.length];
        int counter = 0;
        for (String recipient1 : recipientList) {
            try {
                recipientAddress[counter] = new InternetAddress(recipient1.trim());
            } catch (AddressException e) {
                e.printStackTrace();
            }
            counter++;
        }

    // Sender's email ID needs to be mentioned
    String from = "abc@xyz.com";
    final String username = "abc@xyz.com";//change accordingly
    final String password ="password`enter code here`";//change accordingly

    String host = "smtp.office365.com";
    Properties props = new Properties();
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.ssl.trust", host);
   // props.put("mail.smtp.ssl.enable", "true");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.user", username);



// Get the Session object.
    Session session = Session.getInstance(props,
       new javax.mail.Authenticator() {
          protected PasswordAuthentication getPasswordAuthentication() {
             return new PasswordAuthentication(username, password);
                 }
       });

    try {
               Transport tr = session.getTransport("smtp");
               tr.connect();
               System.out.println("Validating email finished");
                 // Create a default MimeMessage object.
                 Message message = new MimeMessage(session);

                 // Set From: header field of the header.
                 message.setFrom(new InternetAddress(from));

                 // Set To: header field of the header.
                 message.setRecipients(Message.RecipientType.TO, recipientAddress);

                 // Set Subject: header field
                 message.setSubject(subject);

                 // HTML TEXT
                 message.setContent(htmlMsg, "text/html");


                 // Send message
                 Transport.send(message);

                 System.out.println("Sent message successfully....");

    } catch (Exception e) {
        System.out.println("Exception--------------------"+e);
       throw new RuntimeException(e);
    }       

                // TODO Auto-generated constructor stub
}`
请参见例外情况:

The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; 
您可以使用SSL连接到SMTP服务器。您必须将SMTP服务器证书放入WebSphere Application Truststore,以便它能够建立连接。您的Tomcat服务器使用不同的JDK,因此使用不同的truststore

请参阅其他文章,了解如何在WAS中将签名者证书添加到信任库

第二个考虑因素是您应该在WAS中使用MailSession,而不是在代码中硬编码所有邮件服务器数据。这是在JavaEE应用程序中获取邮件会话的推荐方法

如果您不想在完整WAS上开发,那么您应该使用WebSphereLiberty概要文件进行开发,而不是使用Tomcat。它在启动时间和内存占用方面与Tomcat一样轻量级,并且WAS中已经包含了库,因此您不必添加第三方LIB。

请参见例外:

The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; 
您可以使用SSL连接到SMTP服务器。您必须将SMTP服务器证书放入WebSphere Application Truststore,以便它能够建立连接。您的Tomcat服务器使用不同的JDK,因此使用不同的truststore

请参阅其他文章,了解如何在WAS中将签名者证书添加到信任库

第二个考虑因素是您应该在WAS中使用MailSession,而不是在代码中硬编码所有邮件服务器数据。这是在JavaEE应用程序中获取邮件会话的推荐方法

如果您不想在完整WAS上开发,那么您应该使用WebSphereLiberty概要文件进行开发,而不是使用Tomcat。它在启动时间和内存占用方面与Tomcat一样轻量级,并且WAS中已经包含了库,因此您不必添加第三方LIB