Guest User

Untitled

a guest
Apr 25th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.mail.internet.MimeMessage;
  5. public class EmailSend {
  6.  
  7. public static void main(String args[]){
  8. try{
  9. String host ="smtp.gmail.com" ;
  10. final String user = "abc@gmail.com";
  11. final String pass = "password";
  12. String to = "xyz@gmail.com";
  13. String from = "abc@gmail.com";
  14. String subject = "Trial";
  15. String messageText = "Your Is Test Email :";
  16. boolean sessionDebug = false;
  17. Properties props = System.getProperties();
  18.  
  19. props.put("mail.smtp.starttls.enable", "true");
  20. props.put("mail.smtp.host", host);
  21. props.put("mail.smtp.port", "587");
  22. props.put("mail.smtp.auth", "true");
  23. props.put("mail.smtp.starttls.required", "true");
  24.  
  25. java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  26. Session mailSession = Session.getDefaultInstance(props, null);
  27. mailSession.setDebug(sessionDebug);
  28. Message msg = new MimeMessage(mailSession);
  29. msg.setFrom(new InternetAddress(from));
  30. InternetAddress[] address = {new InternetAddress(to)};
  31. msg.setRecipients(Message.RecipientType.TO, address);
  32. msg.setSubject(subject); msg.setSentDate(new Date());
  33. msg.setText(messageText);
  34.  
  35. Transport transport=mailSession.getTransport("smtp");
  36. transport.connect(host, user, pass);
  37. transport.sendMessage(msg, msg.getAllRecipients());
  38. transport.close();
  39. System.out.println("message send successfully");
  40. }catch(Exception ex)
  41. {
  42. System.out.println(ex);
  43. }
  44.  
  45. }
  46. }
  47.  
  48. javax.mail.MessagingException: Can't send command to SMTP host;
  49. nested exception is:
  50. 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
Add Comment
Please, Sign In to add comment