1

在执行以下行时连接到我的邮件服务器时出现以下异常

transport.connect("test.mailserver.com",465,"test.user@test.com","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,"test.user@test.com","testpwd");
    transport.sendMessage(p_msg, p_msg.getAllRecipients());
    transport.close();
}
4

2 回答 2

1

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

因此,如果您对其他答案不满意,请尝试查看您在邮件服务器上安装 SSL 证书等的方式。

于 2010-12-16T06:49:56.240 回答
0

要从 java 发送电子邮件,您需要以下 jar:

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

请尝试使用以下方法从 java 发送电子邮件。此方法将使用 SSL 身份验证发送电子邮件。在下面的方法中,有三个参数: 列出收件人:列出该邮件的所有收件人。subject:此邮件的主题 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();
        }
    }

谢谢,

于 2010-12-16T05:52:37.607 回答