Advertisement
Guest User

Untitled

a guest
Aug 8th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.http.HttpServlet;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8.  
  9. import helper.MailHelper;
  10.  
  11.  
  12. public class MailServlet extends HttpServlet {
  13. private static final long serialVersionUID = 3303982021932755186L;
  14. @Override
  15. protected void doPost(HttpServletRequest request,
  16. HttpServletResponse response) throws ServletException, IOException {
  17. processRequest(request, response);
  18. }
  19. public void processRequest(HttpServletRequest request,
  20. HttpServletResponse response) throws ServletException, IOException {
  21. String email = request.getParameter("mail");
  22. String subject = request.getParameter("subject");
  23. String message = request.getParameter("message");
  24. if (isValidParameters(email, subject, message)) {
  25. try {
  26. MailHelper.sendMail(email, subject, message);
  27. request.getRequestDispatcher("success.jsp").forward(request,
  28. response);
  29. } catch (Exception ex) {
  30. request.getRequestDispatcher("internalError.jsp").forward(
  31. request, response);
  32. }
  33. } else {
  34. request.getRequestDispatcher("error.jsp")
  35. .forward(request, response);
  36. }
  37. }
  38. private boolean isValidParameters(String... parameters) {
  39. boolean isValid = true;
  40. for (String param : parameters) {
  41. if (param == null || param.isEmpty()) {
  42. isValid = false;
  43. break;
  44. }
  45. }
  46. return isValid;
  47. }
  48. }
  49.  
  50. package helper;
  51.  
  52. import java.util.Properties;
  53. import javax.mail.Message;
  54. import javax.mail.MessagingException;
  55. import javax.mail.PasswordAuthentication;
  56. import javax.mail.Session;
  57. import javax.mail.Transport;
  58. import javax.mail.internet.AddressException;
  59. import javax.mail.internet.InternetAddress;
  60. import javax.mail.internet.MimeMessage;
  61.  
  62. public class MailHelper {
  63. //input here your mail credentials
  64. static final String username = "myemail@gmail.com";
  65. static final String password = "mypassword";
  66.  
  67. public static void sendMail(String mail, String subject, String message)
  68. throws AddressException, MessagingException {
  69. Message msg = new MimeMessage(getSession());
  70. msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail));
  71. msg.setSubject(subject);
  72. msg.setText(message);
  73. Transport.send(msg);
  74. }
  75. private static Session getSession() {
  76. Session session = Session.getDefaultInstance(getProperties(),
  77. new javax.mail.Authenticator() {
  78. protected PasswordAuthentication getPasswordAuthentication() {
  79. return new PasswordAuthentication(username, password);
  80. }
  81. });
  82. return session;
  83. }
  84. private static Properties getProperties() {
  85. Properties properties = new Properties();
  86. properties.put("mail.smtp.host", "smtp.gmail.com");
  87. properties.put("mail.smtp.socketFactory.port", "465");
  88. properties.put("mail.smtp.socketFactory.class",
  89. "javax.net.ssl.SSLSocketFactory");
  90. properties.put("mail.smtp.auth", "true");
  91. properties.put("mail.smtp.port", "465");
  92. return properties;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement