Required Jar
To Send an email using your 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.
You can also Download this jar From Here.
Web.xml
Following is the
content of web.xml file:
<?xml version="1.0" encoding="UTF-8"?>Demo Email.jsp struts2 org.apache.struts2.dispatcher.FilterDispatcher struts2 /*
Struts.xml
struts.xml
configuration file as follows:
<?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">/success.jsp /error.jsp
Email.jsp
We have to create Email.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></body> </html>Email Form </head> <body>
Success.jsp
When Email send Successfully then success.jsp is called.
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>Email Success </head> <body> Your email to <s:property value="to"/> was sent successfully. </body> </html>
Error.jsp
When Email send Successfully then error.jsp is called.
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>Email Error </head> <body> There is a problem sending your email to <s:property value="to"/>. </body> </html>
EmailAction.java
The next step is to
create an Action method that takes care of sending the email. Let us create a
new class called EmailAction.java.
Code-
package view; 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; public class EmailAction extends ActionSupport { 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) { EmailAction.properties = properties; } }
Output:-
Give Error when unsuccessful.
it works properly...thanks for that...
ReplyDeleteYour suggestion's are valuable to me.
ReplyDeleteHTTP Status 404 - There is no Action mapped for namespace / and action name emailer.
ReplyDelete--------------------------------------------------------------------------------
type Status report
message There is no Action mapped for namespace / and action name emailer.
description The requested resource (There is no Action mapped for namespace / and action name emailer.) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.16
I have this error.... y??
this error occure when action is not mapped in struts.xml file
DeleteIn this emailer Action is not mapped in Struts.xml
For futher information download source code from this page.
Hi! I got this exception :
ReplyDeletejavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Any suggestions as to how to resolve this error?
Please help me out with this:
ReplyDeletejavax.mail.AuthenticationFailedException:
---------------------------------------------
29 Apr, 2016 9:32:34 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
SEVERE: Dispatcher initialization failed
Unable to load configuration. - bean - jar:file:/D:/EclipsWorkSpace/Dhiraj_WorkSpace/MY_Project/.metadata/.me_tcat7/webapps/Struts2EmailApp/WEB-INF/lib/struts2-core-2.3.4.1.jar!/struts-default.xml:29:72
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)