0
String to="mydaman7291@gmail.com";//change accordingly

//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
        "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

Session sess = Session.getInstance(props,
 new javax.mail.Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication("daman.bonnie07@gmail.com","password");//change accordingly
 }
});

//compose message
try {
 MimeMessage message = new MimeMessage(sess);
 message.setFrom(new InternetAddress("daman.bonnie07@gmail.com"));//change accordingly
 message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
 message.setSubject("Hello");
 message.setText("Testing.......");

 //send message
 Transport.send(message);

 System.out.println("message sent successfully");

} 
catch (MessagingException e) {
    out.println(e);}

I am getting the following error while sending the mail

javax.mail.MessagingException: Exception reading response; 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

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • 1
    Add a property `props.put("mail.pop3s.ssl.trust","*");` and check . – AllTooSir May 29 '13 at 15:49
  • no, I am still getting the same exception – Damanpreet Singh May 29 '13 at 16:01
  • If you had googled the error message you would have found dozens (hundreds) of hits. You have to add the appropriate CA certificate to the Java keystore. See http://www.mikepilat.com/blog/2011/05/adding-a-certificate-authority-to-the-java-runtime/ – Jim Garrison May 29 '13 at 19:12

1 Answers1

0

Try this!

Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.xyz.in");
    props.put("mail.smtp.socketFactory.port", "25");

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "25");
    props.put("mail.smtp.dsn.notify",
              "SUCCESS ORCPT=rfc822;");
    props.put("mail.smtp.dsn.ret", "FULL");
MaheshVarma
  • 2,081
  • 7
  • 35
  • 58