0
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage;
public class EmailSend {

    public static void main(String args[]){
        try{
            String host ="smtp.gmail.com" ;
            final String user = "abc@gmail.com";
            final String pass = "password";
            String to = "xyz@gmail.com";
            String from = "abc@gmail.com";
            String subject = "Trial";
            String messageText = "Your Is Test Email :";
            boolean sessionDebug = false;
                    Properties props = System.getProperties();

            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.required", "true");

            java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            Session mailSession = Session.getDefaultInstance(props, null);
            mailSession.setDebug(sessionDebug);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = {new InternetAddress(to)};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject); msg.setSentDate(new Date());
            msg.setText(messageText);

           Transport transport=mailSession.getTransport("smtp");
           transport.connect(host, user, pass);
           transport.sendMessage(msg, msg.getAllRecipients());
           transport.close();
           System.out.println("message send successfully");
       }catch(Exception ex)
        {
            System.out.println(ex);
        }

    }
}

I have added activation-1.1.1.jar and also mail-1.4.jar and I have also changed the email id settings to * Allow less secure apps: ON

But I get the following exception

javax.mail.MessagingException: Can't send command to SMTP host;
  nested exception is:
    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

I am using eclipse IDE (oxygen 3A)

  • I don't know if that's the correct thing to tag as a duplicate of, as it encourages OP to bypass the cerificate assuming they've already tried to import the cert, when in fact they might just not be aware that they should have imported it in the first place – Zachary Craig Apr 25 '18 at 11:15

1 Answers1

1

The error you are facing is not related to Java mail but SSL certificate. To resolve this error, you will need to import the certificate of your SMTP host into your keystore.

Google provides below mechanism to download the smtp certificate:

openssl s_client -starttls smtp -connect [hostname]:25 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'

Refer to other methods for downloading the certificates on "https://support.google.com/a/answer/6180220?hl=en"

Once you have the certificate with you, you can import the certificate into your java keystore with below command:

keytool -import -file smtpgooglecert.cer -alias smtpgooglecert -keystore keystore.jks

Hope this helps !!

Avinash Sagar
  • 527
  • 4
  • 10