简体   繁体   English

通过SMTP发送邮件时出错

[英]Error in sending mail through smtps

I am getting the below exception while connecting to my mailserver while executing the below line 执行以下行时,连接到邮件服务器时出现以下异常

transport.connect("test.mailserver.com",465,"test.user@test.com","testpwd");

The Exception is: 例外是:

(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

below is the code: 下面是代码:

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();
}

I dimly recall running into something like this myself. 我隐约记得自己遇到了这种情况。 It turned out that I had configured SSL incorrectly by putting the certificates in the server's certificate chain in the wrong order. 原来,我以错误的顺序将证书放入服务器的证书链中,从而错误地配置了SSL。 The SSL stacks in a typical web browser don't care about this, but (apparently) the client-side SSL engine in Java cannot (or will not) cope with a server that presents the chain in the wrong order. 典型的Web浏览器中的SSL堆栈对此并不在乎,但是(显然)Java中的客户端SSL引擎无法(或不会)与以错误顺序显示链的服务器相对应。

So, if you get no luck with other answers, try looking at the way that you have installed the SSL certificate, etc on your mail server. 因此,如果您在其他答案上不走运,请尝试查看在邮件服务器上安装SSL证书等的方式。

For sending an email from java, you need below jars: 要从Java发送电子邮件,您需要以下jar:

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

Please try to use below method to send an email from java. 请尝试使用以下方法从Java发送电子邮件。 This method will send an email using SSL authentication. 此方法将使用SSL身份验证发送电子邮件。 In the below method, there is three parameters: List recipients : list all recipients of this mail. 在下面的方法中,有三个参数:列出收件人:列出此邮件的所有收件人。 subject: subject of this mail messageToSend: message body of the mail. 主题:此邮件的主题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();
        }
    }

Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM