Advertisement
Guest User

Untitled

a guest
Apr 6th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. public class SendMailTLS {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. final String username = "username@gmail.com";
  6. final String password = "password";
  7.  
  8. Properties props = new Properties();
  9. props.put("mail.smtp.auth", "true");
  10. props.put("mail.smtp.starttls.enable", "true");
  11. props.put("mail.smtp.host", "smtp.gmail.com");
  12. props.put("mail.smtp.port", "587");
  13.  
  14. Session session = Session.getInstance(props,
  15. new javax.mail.Authenticator() {
  16. protected PasswordAuthentication getPasswordAuthentication() {
  17. return new PasswordAuthentication(username, password);
  18. }
  19. });
  20.  
  21. try {
  22.  
  23. Message message = new MimeMessage(session);
  24. message.setFrom(new InternetAddress("from-email@gmail.com"));
  25. message.setRecipients(Message.RecipientType.TO,
  26. InternetAddress.parse("to-email@gmail.com"));
  27. message.setSubject("Testing Subject");
  28. message.setText("Dear Mail Crawler,"
  29. + "nn No spam to my email, please!");
  30.  
  31. Transport.send(message);
  32.  
  33. System.out.println("Done");
  34.  
  35. } catch (MessagingException e) {
  36. throw new RuntimeException(e);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement