1 | /*
|
---|
2 | * To change this template, choose Tools | Templates
|
---|
3 | * and open the template in the editor.
|
---|
4 | */
|
---|
5 | package web.servlets;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.io.PrintWriter;
|
---|
9 | import javax.servlet.ServletException;
|
---|
10 | import javax.servlet.http.HttpServlet;
|
---|
11 | import javax.servlet.http.HttpServletRequest;
|
---|
12 | import javax.servlet.http.HttpServletResponse;
|
---|
13 | import org.apache.commons.mail.DefaultAuthenticator;
|
---|
14 | import org.apache.commons.mail.EmailException;
|
---|
15 | import org.apache.commons.mail.SimpleEmail;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | *
|
---|
19 | * @author OEM
|
---|
20 | */
|
---|
21 | public class Email extends HttpServlet {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
---|
25 | * @param request servlet request
|
---|
26 | * @param response servlet response
|
---|
27 | * @throws ServletException if a servlet-specific error occurs
|
---|
28 | * @throws IOException if an I/O error occurs
|
---|
29 | */
|
---|
30 | protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
---|
31 | throws ServletException, IOException {
|
---|
32 | response.setContentType("text/html;charset=UTF-8");
|
---|
33 | PrintWriter out = response.getWriter();
|
---|
34 | try {
|
---|
35 | /* TODO output your page here
|
---|
36 | out.println("<html>");
|
---|
37 | out.println("<head>");
|
---|
38 | out.println("<title>Servlet Email</title>");
|
---|
39 | out.println("</head>");
|
---|
40 | out.println("<body>");
|
---|
41 | out.println("<h1>Servlet Email at " + request.getContextPath () + "</h1>");
|
---|
42 | out.println("</body>");
|
---|
43 | out.println("</html>");
|
---|
44 | */
|
---|
45 | } finally {
|
---|
46 | out.close();
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
|
---|
51 | /**
|
---|
52 | * Handles the HTTP <code>GET</code> method.
|
---|
53 | * @param request servlet request
|
---|
54 | * @param response servlet response
|
---|
55 | * @throws ServletException if a servlet-specific error occurs
|
---|
56 | * @throws IOException if an I/O error occurs
|
---|
57 | */
|
---|
58 | @Override
|
---|
59 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
60 | throws ServletException, IOException {
|
---|
61 | processRequest(request, response);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Handles the HTTP <code>POST</code> method.
|
---|
66 | * @param request servlet request
|
---|
67 | * @param response servlet response
|
---|
68 | * @throws ServletException if a servlet-specific error occurs
|
---|
69 | * @throws IOException if an I/O error occurs
|
---|
70 | */
|
---|
71 | @Override
|
---|
72 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
73 | throws ServletException, IOException {
|
---|
74 | response.setContentType("text/html;charset=UTF-8");
|
---|
75 | PrintWriter out = response.getWriter();
|
---|
76 |
|
---|
77 | final String host = "smtp.gmail.com";
|
---|
78 | final String user = "[email protected]";
|
---|
79 | final String password = "macroniserpassword";
|
---|
80 | final String subject = request.getParameter("subject");
|
---|
81 | final String message = request.getParameter("message");
|
---|
82 | final String lang = request.getParameter("lang");
|
---|
83 |
|
---|
84 | final org.apache.commons.mail.Email email = new SimpleEmail();
|
---|
85 | try {
|
---|
86 | email.setHostName(host);
|
---|
87 | email.setSmtpPort(587);
|
---|
88 | email.setAuthenticator(new DefaultAuthenticator(user, password));
|
---|
89 | email.setTLS(true);
|
---|
90 | email.setFrom(user);
|
---|
91 | email.setSubject(subject);
|
---|
92 | email.setMsg(message);
|
---|
93 | email.addTo(user);
|
---|
94 | email.send();
|
---|
95 |
|
---|
96 | out.println("<html>");
|
---|
97 | out.println("<head>");
|
---|
98 | out.println("<title>Servlet EmailServlet</title>");
|
---|
99 | out.println("</head>");
|
---|
100 | out.println("<body>");
|
---|
101 | final String thankyouMessage = lang.equals("en") ? "Thank you" : "Kia ora";
|
---|
102 | out.println("<h1>" + thankyouMessage + "</h1>");
|
---|
103 | out.println("</body>");
|
---|
104 | out.println("</html>");
|
---|
105 | } catch (EmailException e) {
|
---|
106 | e.printStackTrace();
|
---|
107 | } finally {
|
---|
108 | out.close();
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Returns a short description of the servlet.
|
---|
114 | * @return a String containing servlet description
|
---|
115 | */
|
---|
116 | @Override
|
---|
117 | public String getServletInfo() {
|
---|
118 | return "Short description";
|
---|
119 | }// </editor-fold>
|
---|
120 | }
|
---|