Sharique
Sharique

Reputation: 859

Getting exception while sending a mail using javax mail

Below are the methods which I am using to send an email

 public static void main(String[] args) {
    final String fromEmail = "[email protected]"; //requires valid gmail id
            final String password = "123456"; // correct password for gmail id
            final String toEmail = "[email protected],[email protected]"; 

            System.out.println("TLSEmail Start");
            Properties props = new Properties();
            props.put("mail.smtp.host", "smtp.gmail.com"); 
            props.put("mail.smtp.port", "587"); 
            props.put("mail.smtp.auth", "true"); 
            props.put("mail.smtp.starttls.enable", "true"); 

            Authenticator auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(fromEmail, password);
                }
            };
            Session session = Session.getInstance(props, auth);


    sendEmail(session, toEmail,"TLSEmail Testing Subject", "TLSEmail Testing Body");
    }

     public static void sendEmail(Session session, String toEmail, String subject, String body){
            try
            {
              MimeMessage msg = new MimeMessage(session);
              //set message headers
              msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
              msg.addHeader("format", "flowed");
              msg.addHeader("Content-Transfer-Encoding", "8bit");



              String content = getContent();


              msg.setSubject(subject, "UTF-8");
              msg.setContent(content.toString(), "text/html");

              msg.setSentDate(new Date());

              msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
              System.out.println("Message is ready");
              Transport transport = session.getTransport("smtp");
              transport.connect();
              Transport.send(msg);  

              System.out.println("EMail Sent Successfully!!");
              transport.close();
            }
            catch (Exception e) {
              e.printStackTrace();
            }
        }   

And this is the exception which I suddenly I started getting

 javax.mail.MessagingException: Could not convert socket to TLS;
      nested exception is:
        javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1999)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:709)
        at javax.mail.Service.connect(Service.java:386)
        at javax.mail.Service.connect(Service.java:245)
        at javax.mail.Service.connect(Service.java:194)
        at com.sharique.mail.util.EmailUtil.sendEmail(EmailUtil.java:45)
        at com.sharique.main.TestMain.main(TestMain.java:38)
    Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.ssl.Alerts.getSSLException(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
        at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
        at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
        at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
        at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
        at sun.security.ssl.Handshaker.processLoop(Unknown Source)
        at sun.security.ssl.Handshaker.process_record(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
        at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
        at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:543)
        at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:480)
        at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1994)
        ... 6 more
    Caused by: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.validator.PKIXValidator.doValidate(Unknown Source)
        at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
        at sun.security.validator.Validator.validate(Unknown Source)
        at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
        at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
        at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
        ... 17 more
    Caused by: java.security.cert.CertPathValidatorException: timestamp check failed
        at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(Unknown Source)
        at sun.security.provider.certpath.PKIXCertPathValidator.doValidate(Unknown Source)
        at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(Unknown Source)
        at java.security.cert.CertPathValidator.validate(Unknown Source)
        ... 23 more
    Caused by: java.security.cert.CertificateExpiredException: NotAfter: Sat Apr 04 20:45:55 IST 2015
        at sun.security.x509.CertificateValidity.valid(Unknown Source)
        at sun.security.x509.X509CertImpl.checkValidity(Unknown Source)
        at sun.security.provider.certpath.BasicChecker.verifyTimestamp(Unknown Source)
        at sun.security.provider.certpath.BasicChecker.check(Unknown Source)
        ... 27 more

Code was working fine without any errors, but just few minutes ago it started giving me the above exception. I didn't change the code, so how it is coming I don't know. Please let me know of how to resolve this.

Upvotes: 2

Views: 2838

Answers (4)

user2584198
user2584198

Reputation: 11

One of my app begin to send mail again automatically without any change in the code...

Upvotes: 0

matr07
matr07

Reputation: 106

I was having the exact same issue as well. (Was even showing the same "Not After" message and same time as yours). After changing some configurations around, it started working again, however, I went back to my original configuration and it worked. I believe this was a hiccup in Gmail, which they have since fixed. Have you tried using port 465? That's the port I'm using which is working for me.

Also, here is my config that seems to be sending emails without the exception (This is config in my grails application, but you should be able to port these over to your Java code):

    grails.mail.host = "smtp.gmail.com"
    grails.mail.port = 465
    grails.mail.username = "[email protected]"
    grails.mail.password = "xxxxxxx"
    grails.mail.props = ["mail.smtp.auth":"true",
                         "mail.smtp.socketFactory.port":"465",
                         "mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
                         "mail.smtp.socketFactory.fallback":"false"]

Upvotes: 0

user2584198
user2584198

Reputation: 11

the solution is add that line props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

Upvotes: 1

arpitpanwar
arpitpanwar

Reputation: 309

The exception clearly states what is causing it. The certificate of your mail server seems to have expired, try accessing the server using a browser and check its certificate or try getting any page on the server using wget, you should receive a certificate error.

    Caused by: java.security.cert.CertPathValidatorException: timestamp check failed
    at sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(Unknown Source)
    at sun.security.provider.certpath.PKIXCertPathValidator.doValidate(Unknown Source)
    at sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(Unknown Source)
    at java.security.cert.CertPathValidator.validate(Unknown Source)
    ... 23 more
Caused by: java.security.cert.CertificateExpiredException: NotAfter: Sat Apr 04 20:45:55 IST 2015
    at sun.security.x509.CertificateValidity.valid(Unknown Source)
    at sun.security.x509.X509CertImpl.checkValidity(Unknown Source)
    at sun.security.provider.certpath.BasicChecker.verifyTimestamp(Unknown Source)
    at sun.security.provider.certpath.BasicChecker.check(Unknown Source)
    ... 27 more

Upvotes: 0

Related Questions