Back to: Java Struts Tutorials
Struts 2 Email with Example
In this article, I am going to discuss Struts 2 Email with Examples. Please read our previous article where we discussed Struts 2 Database. In this demo, we will be using a Struts2 application to send an email. For this project, you will require a Gmail ID, as the address and port numbers will correspond to Gmail servers.
Struts 2 Email Example
Step 1: Create a new maven project in Eclipse. We will use Maven to manage the dependencies of struts2. This way, we do not have to import JARs.
Remember to set the packaging to war. This is because we shall be deploying a web application.
Finally, click on the finish button to create the project. The following project directories need to be created:
Step 2: Initially, the pom.xml file should contain the following content:
Modify pom.xml to add the required dependencies and the build configuration:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.dotnet</groupId> <artifactId>strutex</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-convention-plugin</artifactId> <version>6.1.2.1</version> </dependency> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts-taglib</artifactId> <version>1.3.10</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.6.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.11.0</version> <configuration> <source>17</source> <target>17</target> </configuration> </plugin> </plugins> </build> </project>
Step 3: In the src/main/webapp directory, create a new file called index.jsp. Add the following content to the file:
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1" pageEncoding = "ISO-8859-1"%> <%@ taglib prefix = "s" uri = "/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Email Form</title> </head> <body> <form action = "EmailAction" method = "post"> <label for = "from">From: </label><br/> <input type = "text" name = "from"/><br/> <label for = "password">Password: </label><br/> <input type = "password" name = "password"/><br/> <label for = "to">To: </label><br/> <input type = "text" name = "to"/><br/> <label for = "subject">Subject: </label><br/> <input type = "text" name = "subject"/><br/> <label for = "body">Body: </label><br/> <input type = "text" name = "body"/><br/> <input type = "submit" value = "Send Email"/> </form> </body> </html>
Step 4: In the src/main/webapp directory, create a new file called success.jsp. Add the following content to the file:
<%@ page contentType = "text/html; charset = UTF-8" %> <%@ taglib prefix = "s" uri = "/struts-tags" %> <html> <head> <title>Email Sent</title> </head> <body> Your email was sent successfully. </body> </html>
Step 5: In the src/main/webapp directory, create a new file called error.jsp. This file will be displayed if there is an error when the file upload is in progress. Add the following content to the file:
<%@ page contentType = "text/html; charset = UTF-8" %> <%@ taglib prefix = "s" uri = "/struts-tags" %> <html> <head> <title>Error</title> </head> <body> Your email could not be sent. </body> </html>
Step 6: In the src/main/java directory, create a new file called EmailAction.java. The file contains the Java action code. This code should be executed when the “Send Email” button is clicked. Add the following content to the file:
package com.dotnet; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import com.opensymphony.xwork2.ActionSupport; public class EmailAction extends ActionSupport { private String from, to, sub, msg, pass; static Properties properties = new Properties(); static { properties.put("mail.smtp.host", "smtp.gmail.com"); properties.put("mail.smtp.socketFactory.port", "465"); properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.port", "465"); } public String execute() { String ret = SUCCESS; try { Session s = Session.getDefaultInstance(properties, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, pass); } }); Message m = new MimeMessage(s); m.setFrom(new InternetAddress(from)); m.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); m.setSubject(sub); m.setText(msg); Transport.send(m); } catch (Exception e) { ret = ERROR; e.printStackTrace(); } return ret; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getSub() { return sub; } public void setSub(String sub) { this.sub = sub; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public static Properties getProperties() { return properties; } public static void setProperties(Properties properties) { EmailAction.properties = properties; } }
The execute() function tries to send an email from the “from” address to the “to” address. Based on the outcome of this operation, the method returns SUCCESS or ERROR.
Step 7: In the src/main/resources directory, create a new file called struts.xml. The file is responsible for configuring struts2. Add the following content to the file:
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name = "struts.devMode" value = "true" /> <package name = "helloworld" extends = "struts-default"> <action name = "email" class = "com.dotnet.EmailAction" method = "execute"> <result name = "success">/success.jsp</result> <result name = "error">/error.jsp</result> </action> </package> </struts>
Step 8: In the src/main/webapp directory, create a new directory called WEB-INF. In this directory, create a new file called web.xml. This file is responsible for implementing struts2 in the web application. Add the following content to the file:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/web-app_3_0.xsd" id="apache-struts-config-example" version="3.0"> <display-name>Struts Config XML</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
Step 9: Compile and execute the program on the Tomcat server. The following page must open in your default web browser:
Enter the details as required and click on the “Send Email” button. The following webpage must be displayed:
You may check the inbox of the receiving email address. The email should be received. Congratulations! You have successfully sent an email via struts2.
In the next article, I am going to discuss Struts 2 Validation with Examples. Here, in this article, I try to explain Struts 2 Email with Examples and I hope you enjoy this Struts 2 with Email article.