<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this chapter we will teach you how to send an email using your Struts 2 application. For this application, you need to download and install the<b> mail.jar</b> from <b><a href="http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eeplat-419426.html#javamail-1.4.7-oth-JPR" target="_blank" rel="noopener">JavaMail API 1.4.7</a></b> and place the <b>mail.jar</b> file in your <i><b>WEB-INFlib </b></i>folder and then proceed to follow the standard steps of creating action, view and configuration files.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/1212212.png" border="0" /></div>
<h2><b>Create Action:</b></h2>
<p>The next step is to create an Action method that takes care of sending the email. Let us create a new class called <b>MailAction.java</b> with the following contents.</p>
</div>
<pre class="highlight">package com.dineshonjava.struts2.mail.action; 
 
import java.util.Properties; 
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; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class MailAction extends ActionSupport { 
 
 private static final long serialVersionUID = 1L; 
 private String from; 
 private String password; 
 private String to; 
 private String subject; 
 private String body; 
 
 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 session = Session.getDefaultInstance(properties, 
 new javax.mail.Authenticator() { 
 protected PasswordAuthentication 
 getPasswordAuthentication() { 
 return new 
 PasswordAuthentication(from, password); 
 }}); 
 
 Message message = new MimeMessage(session); 
 message.setFrom(new InternetAddress(from)); 
 message.setRecipients(Message.RecipientType.TO, 
 InternetAddress.parse(to)); 
 message.setSubject(subject); 
 message.setText(body); 
 Transport.send(message); 
 } 
 catch(Exception e) 
 { 
 ret = ERROR; 
 e.printStackTrace(); 
 } 
 return ret; 
 } 
 
 public String getFrom() { 
 return from; 
 } 
 
 public void setFrom(String from) { 
 this.from = from; 
 } 
 
 public String getPassword() { 
 return password; 
 } 
 
 public void setPassword(String password) { 
 this.password = password; 
 } 
 
 public String getTo() { 
 return to; 
 } 
 
 public void setTo(String to) { 
 this.to = to; 
 } 
 
 public String getSubject() { 
 return subject; 
 } 
 
 public void setSubject(String subject) { 
 this.subject = subject; 
 } 
 
 public String getBody() { 
 return body; 
 } 
 
 public void setBody(String body) { 
 this.body = body; 
 } 
 
 public static Properties getProperties() { 
 return properties; 
 } 
 
 public static void setProperties(Properties properties) { 
 MailAction.properties = properties; 
 } 
} 
 
</pre>
</div>
<div id="ads-id" align="center"></div>
<p>As seen in the source code above, the <b>MailAction.java</b> has properties that correspond to the form attributes in the <i><b>welcome.jsp </b></i>page given below. These attributes are</p>
<ul style="text-align: left;">
<li><b>from </b>&#8211; The email address of the sender. As we are using Google&#8217;s SMTP, we need a valid gtalk id</li>
<li><b>password </b>&#8211; The password of the above account</li>
<li><b>to </b>&#8211; Who to send the email to?</li>
<li><b>Subject </b>&#8211; subject of the email</li>
<li><b>body </b>&#8211; The actual email message</li>
</ul>
<p>We have not considered any validations on the above fields, validations will be added in the next chapter. Let us now look at the <b>execute()</b> method. The <b>execute() </b>method uses the javax Mail library to send an email using the supplied parameters. If the mail is sent successfully, action returns SUCCESS, otherwise it returns ERROR.</p>
</div>
<h2><b>Create view pages:</b></h2>
<p>Let us write main page JSP file<i><b> welcome.jsp</b></i>, which will be used to collect email related information mentioned above:<br />
<i><b>welcome.jsp</b></i></p>
<pre class="highlight"><;%@ 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>; 
 <;em>;The form below uses Google's SMTP server. 
 So you need to enter a gmail username and password 
 <;/em>; 
 <;form action="emailer" 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>; 
</pre>
<p><i><b>success.jsp</b></i><br />
We will use JSP file <i><b>success.jsp</b></i> which will be invoked in case action returns SUCCESS, but we will have another view file in case of an ERROR is returned from the action.</p>
</div>
<pre class="highlight"><;%@ 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 Success<;/title>; 
<;/head>; 
<;body>; 
 Your email to <;s:property value="to"/>; was sent successfully. 
<;/body>; 
<;/html>; 
</pre>
</div>
<p><i><b>error.jsp</b></i><br />
Following will be the view file <i><b>error.jsp</b></i> in case of an ERROR is returned from the action.</p>
<pre class="highlight"><;%@ 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 Error<;/title>; 
<;/head>; 
<;body>; 
 There is a problem sending your email to <;s:property value="to"/>;. 
<;/body>; 
<;/html>; 
</pre>
</div>
<h2><b>Configuration Files:</b></h2>
<p>Now let us put everything together using the <i><b>struts.xml </b></i>configuration file as follows:</p>
<pre class="highlight"><;?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.enable.DynamicMethodInvocation" value="false" />; 
 <;constant name="struts.devMode" value="false" />; 
 <;constant name="struts.custom.i18n.resources" value="myapp" />; 
 
 <;package name="default" extends="struts-default" namespace="/">; 
 <;action name="emailer" class="com.dineshonjava.struts2.mail.action.MailAction" method="execute">; 
 <;result name="success">;/success.jsp<;/result>; 
 <;result name="error">;/error.jsp<;/result>; 
 <;/action>; 
 <;/package>; 
 
<;/struts>; 
</pre>
</div>
<p>Following is the content of <i><b>web.xml</b></i> file:</p>
<pre class="highlight"><;?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Struts2Mail<;/display-name>; 
 <;filter>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;filter-class>; 
 org.apache.struts2.dispatcher.FilterDispatcher 
 <;/filter-class>; 
 <;/filter>; 
 <;filter-mapping>; 
 <;filter-name>;struts2<;/filter-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/filter-mapping>; 
 <;welcome-file-list>; 
 <;welcome-file>;Welcome.jsp<;/welcome-file>; 
 <;/welcome-file-list>; 
<;/web-app>; 
</pre>
</div>
<p>Now, right click on the project name and click <i><b>Export >; WAR</b></i> File to create a War file. Then deploy this WAR in the Tomcat&#8217;s webapps directory. Finally, start Tomcat server and try to access</p>
<p><b>URL<i> http://localhost:8080/doj/welcome.jsp</i></b><br />
This will give you following screen:</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/32323.png" border="0" /></div>
</div>
<p>Enter the required information and click <b>Send Email </b>button. If everything goes fine then you should see the following page<br />
<i><b>http://localhost:8080/doj/emailer</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com//wp-content/uploads/2013/07/34233.png" border="0" /></div>
<p><b>See the Inbox of <i>admin@dineshonjava.com</i> as following.</b></p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><b><img src="https://dineshonjava.com//wp-content/uploads/2013/07/121212-1.png" border="0" /></b></div>
<p><b><br />
</b> <b>If any error come to sending the mail the following is coming.</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/07/212111.png" border="0" /></div>
<p> ;</p>
</div>
<h2><b>Download Source Code+Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Struts2Mail.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Struts2Mail.zip</a></b></h2>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/struts-2-database-access/">Previous</a> <;<; || <a href="https://dineshonjava.com/struts-2-baby-step-to-learn/">Index </a>|| >;>;<a href="https://dineshonjava.com/struts2-validation-with-example/">Next</a> >;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/struts-2-database-access/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/struts2-validation-with-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '383'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…