A Mailing application in Struts 2 is given below, This application sends an email to the recipients .You need to download and install the mail.jar and place the mail.jar file in your WEB-INF\lib folder and then create action class
Create Action
private static void sendMail(final String username, final String password,String sender, String destination, String subject,String messageText) {
Properties props = new Properties();
props.put(“mail.smtp.auth”, “true”);
props.put(“mail.smtp.starttls.enable”, “true”);
props.put(“mail.transport.protocol”, “smtp”);
props.put(“mail.smtp.host”, “smtp.gmail.com”);
props.put(“mail.smtp.port”, “587”);Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(sender));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(destination));
message.setSubject(subject);
message.setText(messageText);Transport.send(message);
System.out.println(“Done”);
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
Now let us create a JSP main.jsp which contains email related informations.
<%@ 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 Registration</title>
</head>
<body>
<form action=”sentMailAction” method=”post”>
<label for=”sender”>Sender</label><br/> <input type=”text” name=”sender”/><br/>
<label for=”password”>Password</label><br/> <input type=”password” name=”password”/><br/>
<label for=”destination”>Destination</label><br/> <input type=”text” name=”destination”/><br/>
<label for=”subject”>Subject</label><br/> <input type=”text” name=”subject”/><br/>
<label for=”msgText”>MessageText</label><br/> <input type=”text” name=”msgText”/><br/>
<input type=”submit” value=”Send Email”/>
</form>
</body>
</html>
Now we can create a view page to see success,
<%@ 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=”destination”/> was sent successfully.
</body>
</html>
Lets put everything together using struts.xml configuration 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=”sentMailAction” class=”com.sendMail”>
<result name=”success”>/success.jsp</result>
<result name=”error”>/error.jsp</result>
</action>
</package>
</struts>
Leave a Reply