0

I followed this tutorial for sending emails using the Javamail API but it shows me this error when I try to run the code:

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  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
at SendEmail.main(SendEmail.java:64)

I have the javax.mail.jar file in the class path and I double checked the code, it is still not working.

This is the code:

 import java.util.Properties;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;

 public class SendEmail {
 public static void main(String[] args) {
  // Recipient's email ID needs to be mentioned.
  String to = "myemail@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "javamailcoursework@gmail.com";
  final String username = "javamailcoursework@gmail.com";//change accordingly
  final String password = "password";//change accordingly

  // Assuming you are sending email through relay.jangosmtp.net
  String host = "smtp.gmail.com";

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

  // Get the Session object.
  Session session = Session.getInstance(props,
     new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(username, password);
   }
     });

  try {
   // Create a default MimeMessage object.
   Message message = new MimeMessage(session);

   // Set From: header field of the header.
   message.setFrom(new InternetAddress(from));

   // Set To: header field of the header.
   message.setRecipients(Message.RecipientType.TO,
           InternetAddress.parse(to));

   // Set Subject: header field
   message.setSubject("Testing Subject");

   // Now set the actual message
   message.setText("Hello, this is sample for to check send " +
    "email using JavaMailAPI ");

   // Send message
   Transport.send(message);

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

  } catch (MessagingException e) {
     throw new RuntimeException(e);
  }

} }

C. Mauro
  • 151
  • 1
  • 1
  • 7

1 Answers1

0

Add this and try again.

props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Atul
  • 1,536
  • 3
  • 21
  • 37
  • Does not work :( Shows a different error Exception in thread "main" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 – C. Mauro Mar 06 '17 at 21:59
  • Issue is related to certificates.Try this http://stackoverflow.com/questions/9619030/resolving-javax-net-ssl-sslhandshakeexception-sun-security-validator-validatore if it helps. – Atul Mar 06 '17 at 22:01
  • How can I use this for checking emails as well? – C. Mauro Mar 06 '17 at 22:37