Guest User

Untitled

a guest
Nov 26th, 2017
368
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. import Beans.Usuario;
  2. import IOBD.InputOutputBD;
  3. import java.io.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. import javax.mail.MessagingException;
  7. import Servlets.MailUtilLocal;
  8. import java.util.*;
  9.  
  10. public class EmailServlet extends HttpServlet {
  11.  
  12. protected void doPost(HttpServletRequest request,
  13. HttpServletResponse response)
  14. throws ServletException, IOException {
  15. // get parameters from the request
  16. String userId = request.getParameter("userId");
  17. String password = request.getParameter("password");
  18. String nombre = request.getParameter("nombre");
  19. String tipo = request.getParameter("tipo");
  20. String emailAddress = request.getParameter("emailAddress");
  21. // create the User object and write it to a file
  22. Usuario user = new Usuario(userId, password, nombre, tipo, emailAddress);
  23. ServletContext sc = getServletContext();
  24.  
  25. // store the User object in the session
  26. HttpSession session = request.getSession();
  27. session.setAttribute("user", user);
  28.  
  29.  
  30. // send email to user
  31. String to = emailAddress;
  32. String from = "email_list@murach.com";
  33. String subject = "Welcome to our email list";
  34. String body = "Dear " + nombre + ",nn"
  35. + "Thanks for joining our email list. "
  36. + "We'll make sure to send you announcements "
  37. + "about new products and promotions.n"
  38. + "Have a great day and thanks again!nn"
  39. + "Kelly Slivkoffn" + "Mike Murach & Associates";
  40. boolean isBodyHTML = false;
  41. try {
  42. MailUtilLocal.sendMail(to, from, subject, body, isBodyHTML);
  43. } catch (MessagingException e) {
  44. String errorMessage =
  45. "ERROR: Unable to send email. "
  46. + "Check Tomcat logs for details.<br>"
  47. + "NOTE: You may need to configure your "
  48. + "system as described in chapter 15.<br>"
  49. + "ERROR MESSAGE: " + e.getMessage();
  50. request.setAttribute("errorMessage", errorMessage);
  51. this.log("Unable to send email. n"
  52. + "Here is the email you tried to send: n"
  53. + "=====================================n"
  54. + "TO: " + emailAddress + "n"
  55. + "FROM: " + from + "n"
  56. + "SUBJECT: " + subject + "n"
  57. + "n"
  58. + body + "nn");
  59.  
  60.  
  61. }
  62. // forward request and response to JSP page
  63. String url = "/index.jsp";
  64. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
  65. dispatcher.forward(request, response);
  66. }
  67. }
  68.  
  69. import java.util.Properties;
  70. import javax.mail.*;
  71. import javax.mail.internet.*;
  72.  
  73. public class MailUtilLocal {
  74.  
  75. public static void sendMail(String to, String from, String subject, String body, boolean bodyIsHTML)
  76. throws MessagingException {
  77. // 1 - get a mail session
  78. Properties props = new Properties();
  79. props.put("mail.transport.protocol", "smtp");
  80. props.put("mail.smtp.host", "localhost");
  81. props.put("mail.smtp.port", 25);
  82. Session session = Session.getDefaultInstance(props);
  83. session.setDebug(true);
  84. // 2 - create a message
  85. Message message = new MimeMessage(session);
  86. message.setSubject(subject);
  87. if (bodyIsHTML) {
  88. message.setContent(body, "text/html");
  89. } else {
  90. message.setText(body);
  91. }
  92. // 3 - address the message
  93. Address fromAddress = new InternetAddress(from);
  94. Address toAddress = new InternetAddress(to);
  95. message.setFrom(fromAddress);
  96. message.setRecipient(Message.RecipientType.TO, toAddress);
  97. // 4 - send the message
  98. Transport.send(message);
  99. }
  100. }
  101.  
  102. String path = sc.getRealPath("/WEB-INF/EmailList.txt");
  103. UserIO.addRecord(user, path);
  104.  
  105. Properties properties = System.getProperties();
  106. properties.setProperty("mail.smtp.host", host);
  107. properties.put("mail.smtp.auth", "true");
  108. properties.put("mail.smtp.starttls.enable", "true");
  109. properties.put("mail.smtp.ssl.enable", "true");
Add Comment
Please, Sign In to add comment