Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. public class EmailUtility {
  2. public static void sendEmail(String host, String port, final String userName, final String password,
  3. String toAddress, String subject, String message) throws AddressException, MessagingException {
  4.  
  5. // setting SMTP server properties
  6. Properties properties = new Properties();
  7. properties.put("mail.smtp.host", host);
  8. properties.put("mail.smtp.port", port);
  9. properties.put("mail.smtp.auth", "true");
  10. properties.put("mail.smtp.starttls.enable", "true");
  11. Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
  12. protected PasswordAuthentication getPasswordAuthentication() {
  13. return new PasswordAuthentication(userName, password);
  14. }
  15. });
  16. // creates a new e-mail message
  17. Message msg = new MimeMessage(session);
  18.  
  19. msg.setFrom(new InternetAddress(userName));
  20. InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
  21. msg.setRecipients(Message.RecipientType.TO, toAddresses);
  22. msg.setSubject(subject);
  23. msg.setSentDate(new Date());
  24. msg.setText(message);
  25.  
  26. // sending the e-mail
  27. Transport.send(msg);
  28.  
  29. }
  30.  
  31. <context-param>
  32. <param-name>host</param-name>
  33. <param-value>smtp.gmail.com</param-value>
  34. </context-param>
  35.  
  36. <context-param>
  37. <param-name>port</param-name>
  38. <param-value>587</param-value>
  39. </context-param>
  40.  
  41. <context-param>
  42. <param-name>user</param-name>
  43. <param-value>test.123@gmail.com</param-value>
  44. </context-param>
  45. <context-param>
  46. <param-name>pass</param-name>
  47. <param-value>12345698</param-value>
  48. </context-param>
  49.  
  50. public void init() {
  51. // reading SMTP server setting from web.xml file
  52. ServletContext context = getServletContext();
  53. host = context.getInitParameter("host");
  54. port = context.getInitParameter("port");
  55. user = context.getInitParameter("user");
  56. pass = context.getInitParameter("pass");
  57. }
  58.  
  59. protected void doPost(HttpServletRequest request,
  60. HttpServletResponse response) throws ServletException, IOException {
  61. // reading form fields
  62. String recipient = request.getParameter("recipient");
  63. String subject = request.getParameter("subject");
  64. String content = request.getParameter("content");
  65. System.out.println(recipient+" sub "+subject+" content "+content);
  66.  
  67. String resultMessage = "";
  68.  
  69. try {
  70. EmailUtility.sendEmail(host, port, user, pass, recipient, subject,
  71. content);
  72. resultMessage = "The e-mail was sent successfully";
  73. } catch (Exception ex) {
  74. ex.printStackTrace();
  75. resultMessage = "There were an error: " + ex.getMessage();
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement