简体   繁体   中英

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. 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.

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.

For sending an email from java, you need below jars:

  • 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. This method will send an email using SSL authentication. 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.

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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