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");


Saturday 6 February 2016

How to remove whitespace in a String in Java

The TRIM method in String class just removes the white-spaces that are there at the start and end of the String.  It does not remove the white-spaces which are available in any other position.

Below is the code snippet to remove the white space,

String demo1 = "All is well";
Demo1 = demo1.replaceAll("\\s", ""); //or demo1 = demo1.replaceAll("\\s+", "");
System.out.println("Printing String ::: " + demo1);

Output:

Printing String ::: Alliswell

Note: The replaced string should be assigned to same variable or to a different variable in-order to get the updated value.

Below is the error instance,

String demo2 = "All is well";
Demo2.replaceAll("\\s", "");
System.out.println("Printing String ::: " + demo2);

Output:

Printing String ::: All is well

The variable “demo2” will remain unchanged since the data is not assigned after replacement.