Sending Email with Attachment:-
To Send an email with Attachment using Struts 2 application. We required mail.jar
from JavaMail API 1.4.4 and place the mail.jar file in
your WEB-INF\lib folder where other jar files are placed.
Web.xml
Following is the
content of web.xml file,web.xml file have save at
WebContent\WEB-INF.
<?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>EmailDemo1</display-name>
<welcome-file-list>
<welcome-file>Login.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
Struts.xml
struts.xml configuration
file as follows,struts.xml have to save at src folder
<?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.multipart.maxSize"
value="10485760" /> <!-- 10 MB -->
<package name="EmailDemo1"
extends="struts-default">
<global-results>
<result name="error">/Error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="error"
/>
</global-exception-mappings>
<action name="doSendEmail"
class="vaibhav.SendEmailAction"
method="sendEmail">
<!--
SMTP configuration -->
<param name="host">smtp.gmail.com</param>
<param name="port">587</param>
<!--
End of SMTP configuration -->
<interceptor-ref name="fileUpload">
<param name="allowedTypes">*/*</param> <!-- all file types -->
<param name="maximumSize">4194304</param> <!-- 4 MB -->
</interceptor-ref>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params"/>
<interceptor-ref name="validation"
/>
<interceptor-ref name="workflow"
/>
<interceptor-ref name="exception"/>
<result name="success"
type="redirect">/Result.jsp</result>
<result name="input">/EmailForm.jsp</result>
</action>
<action name="login"
class="vaibhav.SendEmailAction" method="Login">
<result name="success">/EmailForm.jsp</result>
</action>
</package>
</struts>
All the JSP Files have to save at WebContent folder.
Login.jsp
We have to create Login.jsp
in the WebContent folder, to send the Email.
Code-
<%@ 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>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
<center>
<s:form action="login"
name="f1" enctype="multipart/form-data"
method="post">
<s:textfield id="u"
name="userName" size="20"
label="Email Id" /><br/>
<s:password id="p"
name="password" size="20"
label="Password" /><br/>
<s:submit value="Login"
align="center"/>
</s:form>
</center>
</body>
</html>
EmailForm.jsp
When Login Successfully then EmailForm.jsp is
called.
Code-
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Struts2 - Email
application</title>
</head>
<body>
<center>
<h1>Struts2 - Send
e-mail with attachment</h1>
<s:form action="doSendEmail"
enctype="multipart/form-data" method="post">
<table border="0"
width="80%" align="center">
<tr>
<td>
<s:textfield name="recipient" size="65"
label="Recipient Address" />
</td>
</tr>
<tr>
<td>
<s:textfield name="subject" size="65"
label="Subject" />
</td>
</tr>
<tr>
<td>
<s:textarea cols="50" rows="10"
name="message" label="Message"
/>
</td>
</tr>
<tr>
<td>
<s:file name="fileUpload"
size="60" label="Attach
file" />
</td>
</tr>
<tr>
<td>
<s:submit value="Send
E-mail" align="center"
/>
</td>
</tr>
</table>
<input type="hidden"
id="h1" name="userName1"
value="<s:property value="userName"
/>"
>
<input type="hidden"
id="h2" name="password1"
value="<s:property value="password"
/>">
</s:form>
</center>
</body>
</html>
Result.jsp
When Email send Successfully then Result.jsp
is called.
Code-
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Send Email Result</title>
</head>
<body>
<center>
<h2>The email was sent
successfully!</h2>
</center>
</body>
</html>
Error.jsp
When Email send Successfully then error.jsp
is called.
Code-
<%@ page language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ 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>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>Send Email Error</title>
</head>
<body>
<center>
<h2>There were a
problem while sending an e-mail to: <s:property value="recipient"/></h2>
<h3>Error details:</h3>
<h4><s:property value="exception" /></h4>
</center>
</body>
</html>
0 comments :
Post a Comment