簡體   English   中英

javax.mail.MessagingException:無法將命令發送到SMTP主機

[英]javax.mail.MessagingException: Can't send command to SMTP host

嘗試從Java郵件API發送郵件時,我面臨以下異常: javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: javax.net.ssl.SSLHandshakeException: com.ibm.jsse2.util.j: PKIX path building failed: java.security.cert.CertPathBuilderException: PKIXCertPathBuilderImpl could not build a valid CertPath.; internal cause is: java.security.cert.CertPathValidatorException: The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; internal cause is: java.security.cert.CertPathValidatorException: Certificate chaining error javax.mail.MessagingException: Can't send command to SMTP host; nested exception is: javax.net.ssl.SSLHandshakeException: com.ibm.jsse2.util.j: PKIX path building failed: java.security.cert.CertPathBuilderException: PKIXCertPathBuilderImpl could not build a valid CertPath.; internal cause is: java.security.cert.CertPathValidatorException: The certificate issued by CN=Baltimore CyberTrust Root, OU=CyberTrust, O=Baltimore, C=IE is not trusted; internal cause is: java.security.cert.CertPathValidatorException: Certificate chaining error

下面是我的Java代碼。 相同的代碼在我的本地系統上運行良好。 當我在tomcat服務器上發布Web應用程序,但是相同的代碼不起作用時。當我在IBM WebSphere Application Server上部署它時。

它顯示了以上異常,

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,因此使用的是不同的信任庫。

請參閱其他文章,如何在WAS中將簽署者證書添加到信任庫。

第二個考慮因素是您應該在WAS中使用MailSession,而不是對代碼中的所有郵件服務器數據進行硬編碼。 這是在Java EE應用程序中獲取郵件會話的推薦方法。

如果您不想使用完整的WAS進行開發,則應使用WebSphere Liberty概要文件而不是Tomcat進行開發。 它在啟動時間和內存占用方面與Tomcat一樣輕巧,並且WAS中已經包含了庫,因此您不必添加第三方庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM