Last active
February 1, 2020 07:31
-
-
Save teguhteja/b60d29ced9aa14f8ba99595f338f5821 to your computer and use it in GitHub Desktop.
Email Sending Servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package email; | |
import java.io.IOException; | |
import javax.servlet.ServletContext; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
/** | |
* A servlet that takes message details from user and send it as a new e-mail | |
* through an SMTP server. | |
* | |
* @author www.codejava.net | |
* | |
*/ | |
@WebServlet("/EmailSendingServlet") | |
public class EmailSendingServlet extends HttpServlet { | |
private String host; | |
private String port; | |
private String user; | |
private String pass; | |
public void init() { | |
// reads SMTP server setting from web.xml file | |
ServletContext context = getServletContext(); | |
host = context.getInitParameter("host"); | |
port = context.getInitParameter("port"); | |
user = context.getInitParameter("user"); | |
pass = context.getInitParameter("pass"); | |
} | |
protected void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// reads form fields | |
String subject = request.getParameter("subject"); | |
String recipient = request.getParameter("email"); | |
String content = request.getParameter("message"); | |
String resultMessage = ""; | |
try { | |
EmailUtility.sendEmail(host, port, user, pass, recipient, subject,content); | |
resultMessage = "The e-mail was sent successfully"; | |
} catch (Exception ex) { | |
ex.printStackTrace(); | |
resultMessage = "There were an error: " + ex.getMessage(); | |
} finally { | |
response.getWriter().print("Send email to " + recipient + "<br/>with subject : " | |
+subject+"<br/>Message : "+content); | |
response.getWriter().print("<br/>Status : "+resultMessage); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> | |
<session-config> | |
<session-timeout> | |
30 | |
</session-timeout> | |
</session-config> | |
<context-param> | |
<param-name>host</param-name> | |
<param-value>smtp.gmail.com</param-value> | |
</context-param> | |
<context-param> | |
<param-name>port</param-name> | |
<param-value>587</param-value> | |
</context-param> | |
<context-param> | |
<param-name>user</param-name> | |
<param-value>USER_EMAIL</param-value> | |
</context-param> | |
<context-param> | |
<param-name>pass</param-name> | |
<param-value>USER_PASSWORD</param-value> | |
</context-param> | |
<welcome-file-list> | |
<welcome-file>send_email.jsp</welcome-file> | |
</welcome-file-list> | |
</web-app> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment