• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

Mailing Application in Struts2

June 5, 2012 By j2eereference Leave a Comment

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

 Create an Action class that contains method for sending the email. Let us create a new class  called sendMail.java with the following contents.

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>

Filed Under: Struts

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.