Monday, 8 February 2016

Unable to find valid certification path to requested target

Have you ever faced the below issue while using java mail api, when hitting a mail server? Then this is the right place to find solution. I have been facing this issue for quite a long time and found this fix resolved my exception. Hope this will save your time too.

Problem Description:

            Getting exception when using java mail api to hit the mail server for sending mail across.

Exception occurred:

DEBUG: getProvider() returning provider protocol=smtp; type=javax.mail.Provider$Type@b38caaf; class=com.sun.mail.smtp.SMTPTransport; vendor=Sun Microsystems, Inc
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
220 smtp.gmail.com ESMTP 74sm47150462pfs.33 - gsmtp
DEBUG SMTP: connected to host "smtp.gmail.com", port: 587

EHLO MITHRAJITH
250-smtp.gmail.com at your service, [180.215.123.42]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250 SMTPUTF8
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SMTPUTF8", arg ""
STARTTLS
220 2.0.0 Ready to start TLS
EHLO MITHRAJITH
javax.mail.SendFailedException: Send failure (javax.mail.MessagingException: Can't send command to SMTP host (javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target))
            at javax.mail.Transport.send(Transport.java:163)
            at javax.mail.Transport.send(Transport.java:48)
            at com.rooster.hcmone.mail.HCMOneMailer.sendMail(HCMOneMailer.java:194)
            at com.rooster.hcmone.mail.HCMOneMailer.sendAlertEmail2(HCMOneMailer.java:333)
            at com.rooster.hcmone.action.EmpSpaceAction.mailSharedToAdmin2(EmpSpaceAction.java:2821)
            at com.rooster.hcmone.action.EmpSpaceAction.resendHirechecklistDoc(EmpSpaceAction.java:1522)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:606)
            at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
            at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
            at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)

Solution:

Adding the below properties to mail configuration will fix the issue.
props.put("mail.smtp.starttls.enable", "true");
                        props.put("mail.smtp.auth", "true");


No comments:

Post a Comment