Guest User

Untitled

a guest
Apr 30th, 2018
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. import java.util.*;
  2. import javax.mail.*;
  3. import javax.mail.internet.*;
  4. import javax.activation.*;
  5.  
  6. public class NotificationEmail{
  7.  
  8. private String from = "email@host.com";
  9. private String to = "emailto@host.com";
  10.  
  11. // Authentication Parameters
  12. String host = "smtp.gmail.com";
  13. String port = "465";
  14. String username = "username";
  15. String password = "password";
  16.  
  17. public void sendMail(String message, String subject ,String to){
  18. Properties props = new Properties();
  19. props.setProperty("mail.transport.protocol", "smtps");
  20. props.setProperty("mail.smtps.auth", "true");
  21. props.setProperty("mail.host", host);
  22. props.setProperty("mail.port", port);
  23. props.setProperty("mail.user", username);
  24. props.setProperty("mail.password", password);
  25. Session session = Session.getDefaultInstance(props, null);
  26. Transport transport = session.getTransport("smtp");
  27. MimeMessage mimeMessage = new MimeMessage(session);
  28. Multipart multiPart = new MimeMultipart();
  29. mimeMessage.setSubject(subject);
  30. mimeMessage.addRecipient(RecipientType.TO, new InternetAddress(toAddress));
  31. MimeBodyPart textBodyPart = new MimeBodyPart();
  32. textBodyPart.setContent(message, "text/plain");
  33. multiPart.addBodyPart(textBodyPart);
  34. mimeMessage.setContent(multiPart);
  35. mimeMessage.setFrom(new InternetAddress(fromAddress));
  36. transport.connect();
  37. transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
  38. transport.close();
  39. }
  40.  
  41. }
Add Comment
Please, Sign In to add comment