Home Struts Struts Struts2.2.1 Shoppingcart Mailing Application in Struts2.2.1

Ask Questions?

View Latest Questions


 
 

Mailing Application in Struts2.2.1
Posted on: March 31, 2011 at 12:00 AM
Advertisement
In this tutorial you will learn how to send an email in struts

Mailing Application in Struts2.2.1

A Mailing application in struts is given below, This application sends an email to the recipients you specify on the local SMTP server.

To run this example you fist need to install and run the SMTP server.

To write a method to send an e-mail see the steps given below

1. Set the host smtp address like
    String SMTPHost = "192.168.14.123";

2. Create properties object
    Properties properties = new Properties();

3. Set mail SMTP host in properties
    properties.put("mail.smtp.host", SMTPHost);

4. get the Session
    Session session = Session.getDefaultInstance(properties, null);

5. create a message
    Message msg = new MimeMessage(session);

6. Set sender and Recipients address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);

7. Add all recipients
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) {
    addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);

8. Set e-mail header
    msg.addHeader("MailingApplication", "Struts 2 Mailing Application");

9. Set the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/html");
    Transport.send(msg);

An example for sending an email in struts is given below

sendMail.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
	<title><s:text name="Mailling Application" /></title>
<s:head />
</head>
<body>
	<h1 align="center">Mailing Application</h1>
	<center> <s:form action="send">
	<s:textfield name="mailTo" label="Send To" />
	<s:textfield name="subject" label="Subject" />
	<s:textarea name="message" label="Write Message"/>
	<s:submit value="Send" />
	</s:form></center>
</body>
</html>

MailerForm.java

package roseindia.model;

import java.io.Serializable;

public class MailerForm implements Serializable {

	public MailerForm() {

	}

	private String mailTo;
	private String subject;
	private String message;

	public String getMailTo() {
		return mailTo;
	}

	public void setMailTo(String mailTo) {
		this.mailTo = mailTo;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

Mailer.java

package roseindia.action;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import roseindia.model.MailerForm;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class Mailer extends ActionSupport implements ModelDriven {

	private static final long serialVersionUID = 1L;

	MailerForm model;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String[] recipients = new String[] { model.getMailTo() };
		String subject = model.getSubject();
		String message = model.getMessage();
		String from = "b1@localhost";

		if (model.getMailTo() != "" && model.getMessage() != ""
				&& model.getSubject() != "") {
			// Sending e-mail
			sendMail(recipients, subject, message, from);

			System.out.println("Mail Sent");
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	@Override
	public MailerForm getModel() {
		// TODO Auto-generated method stub
		model = new MailerForm();
		return model;
	}

	@Override
	public void validate() {
		// TODO Auto-generated method stub

		// Code for validation

		if (model.getMailTo() == "" || model.getMailTo() == null) {
			addFieldError("mailTo", getText("mailTo"));
		}

		if (model.getSubject() == "" || model.getSubject() == null) {
			addFieldError("subject", getText("subject"));
		}

		if (model.getMessage() == "" || model.getMessage() == null) {
			addFieldError("message", getText("message"));
		}

		super.validate();
	}

	// Method for sending e-mail
	public void sendMail(String recipients[], String subject, String message,
			String from) throws MessagingException {
		boolean debug = false;

		// Set the host smtp address like String SMTPHost = "192.168.14.123";
		String SMTPHost = "SMTP Server Host address";

		// Create properties object
		Properties properties = new Properties();

		// Set mail SMTP host in properties
		properties.put("mail.smtp.host", SMTPHost);

		// getting Session
		Session session = Session.getDefaultInstance(properties, null);
		session.setDebug(debug);

		// create a message
		Message msg = new MimeMessage(session);

		// Setting sender and Recipients address
		InternetAddress addressFrom = new InternetAddress(from);
		msg.setFrom(addressFrom);

		// Adding all recipients
		InternetAddress[] addressTo = new InternetAddress[recipients.length];
		for (int i = 0; i < recipients.length; i++) {
			addressTo[i] = new InternetAddress(recipients[i]);
		}
		msg.setRecipients(Message.RecipientType.TO, addressTo);

		// Setting e-mail header
		msg.addHeader("MailingApplication", "Struts 2 Mailing Application");

		// Setting the Subject and Content Type
		msg.setSubject(subject);
		msg.setContent(message, "text/html");
		Transport.send(msg);
	}
}

Mailer-validation.xml

<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>
	<field name="mailTo">
	<field-validator type="requiredstring">
	<message key="mailTo" />
	</field-validator>
</field>

<field name="subject">
	<field-validator type="requiredstring">
	<message key="subject" />
	</field-validator>
</field>

<field name="message">
	<field-validator type="requiredstring">
	<message key="message" />
	</field-validator>
</field>

</validators>

Mailer.properties

mailTo = Please Specify Recipient email
subject = Please Specify Subject
message = Please Specify Message

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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />

<package name="roseindia" namespace="" extends="struts-default">

<action name="mailPage">
<result>pages/sendMail.jsp</result>
</action>

<action name="send" class="roseindia.action.Mailer">
<result name="success">pages/confirmation.jsp</result>
<result name="input">pages/sendMail.jsp</result>
</action>

</package>

</struts>



When you run this application it will display message as shown below:


Download Select Source Code

Related Tags for Mailing Application in Struts2.2.1:


DMCA.com