2010-12-16 146 views
1

我得到下面的异常,同时连接到我的邮件服务器在执行以下线错误通过发送邮件SMTPS

transport.connect("test.mailserver.com",465,"[email protected]","testpwd"); 

唯一的例外是:

(javax.mail.MessagingException) javax.mail.MessagingException: Exception reading response; 
nested exception is: 
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: subject/issuer name chaining check failed 
下面

是代码:

protected static Session initializeSession(MailMessage p_msg) throws Exception{ 

    //Get the SMTP Host 
    Properties prop = System.getProperties(); 
    prop.put("mail.smtps.host", "test.mailserver.com"); 
    prop.put("mail.transport.protocol", "smtps"); 
    prop.put("mail.smtps.auth", true); 

    Session session = Session.getInstance(prop,null); 
    session.setDebug(p_msg.getDebugMode()); 
    return session; 
} 
protected static void sendMessage(MimeMessage p_msg) throws Exception{ 

    Properties prop = System.getProperties(); 

    Session session = Session.getDefaultInstance(prop, null); 
    Transport transport = session.getTransport("smtps"); 
     transport.connect("test.mailserver.com",465,"[email protected]","testpwd"); 
    transport.sendMessage(p_msg, p_msg.getAllRecipients()); 
    transport.close(); 
} 

回答

0

对于从Java发送电子邮件,您需要下面的罐子:

  • mail.jar
  • geronimo-javamail-transport-1.1.1.jar
  • geronimo-javamail_1.3.1_spec-1.1.jar

请尝试使用下面的方法来从Java发送电子邮件。该方法将使用SSL认证发送电子邮件。在下面的方法中,有三个参数: 列表收件人:列出此邮件的所有收件人。 主题:此邮件主题 messageToSend:邮件正文。

public void sendMail(List<String> recipents,String subject,String messageToSend) 
    { 
     setParameters(); 
     try { 
      Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); 
      Properties props = new Properties(); 
      props.setProperty("mail.transport.protocol", "smtp"); 
      props.setProperty("mail.host", "smtp.gmail.com"); 

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

      props.put("mail.debug", "true"); 
      props.put("mail.smtp.socketFactory.port", "465"); 

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

      javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props,new javax.mail.Authenticator() 
      { 
       protected PasswordAuthentication getPasswordAuthentication() 
       { 
        return new PasswordAuthentication(Your GmailID,your GMAIL Password); 
       } 
      }); 
      mailSession.setDebug(true); 
      Transport transport = mailSession.getTransport(); 
      InternetAddress addressFrom = new InternetAddress(fromEmailAddress); 
      MimeMessage message = new MimeMessage(mailSession); 
      message.setSender(addressFrom); 
      message.setSubject(subject); 
      message.setContent(messageToSend, "text/plain"); 

      InternetAddress[] addressTo = new InternetAddress[recipents.size()]; 
      for (int i = 0; i < recipents.size(); i++) { 
       addressTo[i] = new InternetAddress(recipents.get(i)); 
      } 
      message.setRecipients(Message.RecipientType.TO, addressTo); 

      transport.connect(); 
      transport.send(message); 
      transport.close(); 
     } catch (MessagingException e) { 
      e.printStackTrace(); 
     } 
    } 

感谢,

+0

嗨Kandarp,谢谢你的回复..我添加了你提到的两个罐子。 – 2010-12-16 09:39:24

+0

我添加了你提到的两个罐子,并尝试使用它的代码,它引发了一个例外:“连接到smtp.gmail.com的错误,465”用于gmail,当我尝试这个为我的邮件服务器抛出以前的异常(javax。 mail.MessagingException)javax.mail.MessagingException:异常读取响应;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径验证失败:java.security.cert.CertPathValidatorException:主题/颁发者名称链接检查失败 – 2010-12-16 09:45:14

1

我依稀记得运行到这样的事情我自己。事实证明,我通过将证书放在服务器证书链中的顺序不正确而错误地配置了SSL。典型的Web浏览器中的SSL堆栈并不关心这一点,但(显然)Java中的客户端SSL引擎不能(或不会)处理以错误顺序呈现链的服务器。

因此,如果您没有其他答案的运气,请尝试查看您在邮件服务器上安装SSL证书的方式。