Struts 2.x : [A Framework from ASF] :: Struts 2.x with Email
Struts 2.x with Email |
Example for Struts 2.x with Email
Example for Struts 2.x with Email |
Project Structure
JavaskoolEmailer.java
package com.javaskool.struts;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaskoolEmailer
{
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 sendMail(final String from, final String password, String to, String subject, String body)
{
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.setRecipients(Message.RecipientType.CC,InternetAddress.parse(to));
//message.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
Transport.send(message);
}
catch(Exception e)
{
ret="error";
e.printStackTrace();
}
return ret;
}
}
EmailerAction.java
package com.javaskool.struts;
import com.opensymphony.xwork2.ActionSupport;
public class EmailerAction extends ActionSupport{
private String from;
private String password;
private String to;
private String subject;
private String body;
public String execute() {
JavaskoolEmailer emailer=new JavaskoolEmailer();
return emailer.sendMail(from,password,to,subject,body);
}
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;
}
}
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Email Form</title>
</head>
<body>
<h2>Struts 2.x - Using gmail account for sending mail..</h2>
<form action="emailer" method="post">
<h2>Sing In</h2>
<pre>
<label for="from">
From :</label><input type="text" name="from"/>
<label for="password">
Password :</label><input type="password" name="password"/>
<b>Compose Mail</b>
<label for="To">
To :</label><input type="text" name="to"/>
<label for="Subject">
Subject :</label><input type="text" name="subject"/>
<label for="body">
Message :</label><textarea cols="30" rows="5" name="body"></textarea><br>
<input type="submit" value="Send Email"/><br>
</pre>
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>Email Success</title>
</head>
<body>
Your Email to <s:property value="to"/> has been sent successfully.
</body>
</html>
failure.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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 problem in sending Email.
</body>
</html>
struts.xml
<?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"></constant>
<package name="p1" extends="struts-default">
<action name="emailer" class="com.javaskool.struts.EmailerAction"
method="execute">
<result name="success">/success.jsp</result>
<result name="error">/failure.jsp</result>
</action>
</package>
</struts>
jar files required in lib folder
Note: List of jar files differ as per project
commons-fileupload-1.2.2
commons-io-2.0.1
commons-lang-2.4
commons-lang3-3.1
commons-logging-1.1.1
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.5
struts2-core-2.3.4
xwork-core-2.3.4
Click Here to download all jar files required for basic Struts 2.x Application
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>07_Struts2WithEmail</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>s2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>s2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Output
If you enter invalid authentication, will return below screen.
Click here to download complete code with jar files [4.2 MB]
Recent Comments