Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  2. nested exception is:
  3. 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
  4. at SendEmail.main(SendEmail.java:64)
  5.  
  6. import java.util.Properties;
  7. import javax.mail.Message;
  8. import javax.mail.MessagingException;
  9. import javax.mail.PasswordAuthentication;
  10. import javax.mail.Session;
  11. import javax.mail.Transport;
  12. import javax.mail.internet.InternetAddress;
  13. import javax.mail.internet.MimeMessage;
  14.  
  15. public class SendEmail {
  16. public static void main(String[] args) {
  17. // Recipient's email ID needs to be mentioned.
  18. String to = "myemail@gmail.com";
  19.  
  20. // Sender's email ID needs to be mentioned
  21. String from = "javamailcoursework@gmail.com";
  22. final String username = "javamailcoursework@gmail.com";//change accordingly
  23. final String password = "password";//change accordingly
  24.  
  25. // Assuming you are sending email through relay.jangosmtp.net
  26. String host = "smtp.gmail.com";
  27.  
  28. Properties props = new Properties();
  29. props.put("mail.smtp.auth", "true");
  30. props.put("mail.smtp.starttls.enable", "true");
  31. props.put("mail.smtp.host", "smtp.gmail.com");
  32. props.put("mail.smtp.port", "587");
  33.  
  34. // Get the Session object.
  35. Session session = Session.getInstance(props,
  36. new javax.mail.Authenticator() {
  37. protected PasswordAuthentication getPasswordAuthentication() {
  38. return new PasswordAuthentication(username, password);
  39. }
  40. });
  41.  
  42. try {
  43. // Create a default MimeMessage object.
  44. Message message = new MimeMessage(session);
  45.  
  46. // Set From: header field of the header.
  47. message.setFrom(new InternetAddress(from));
  48.  
  49. // Set To: header field of the header.
  50. message.setRecipients(Message.RecipientType.TO,
  51. InternetAddress.parse(to));
  52.  
  53. // Set Subject: header field
  54. message.setSubject("Testing Subject");
  55.  
  56. // Now set the actual message
  57. message.setText("Hello, this is sample for to check send " +
  58. "email using JavaMailAPI ");
  59.  
  60. // Send message
  61. Transport.send(message);
  62.  
  63. System.out.println("Sent message successfully....");
  64.  
  65. } catch (MessagingException e) {
  66. throw new RuntimeException(e);
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement