Advertisement
Guest User

Untitled

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