javax.mail.MessagingException:无法向SMTP主机发送命令

时间:2016-02-03 07:58:39

标签: java websphere javamail

尝试从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

以下是我的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
}`

1 个答案:

答案 0 :(得分:2)

见例外:

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中包含了库,因此您不必添加第三方库。