Apr 9, 2010

Mail Support in Spring Framework

This article describes how we can send the mail using the Spring Framework. The Spring framework provides simplified API’s and plug-ins for full e-mail support.

In this age of global networks electronic mail plays an important role in all the day to day activities such as sending a notification, business orders or any periodic report of a product. The java technology provides API’s for creating applications with mail support. The Java Mail API provides classes and methods for creating mail applications. Although it is powerful API but it is very complex for sending simple mails using this API is a difficult task. Moreover the Java Mail API is dependent on the system properties; so testing the application codes that directly use Java Mail API is difficult.

The Spring framework provides simplified API’s and plug-ins for full e-mail support. The Spring API for mail support is divided into two parts: one provides the basic mail support and other provides more complex mail support. While implementing the e-mail support with the Spring framework you can use either Java Mail API based implementation or the Mail Message class of the com.oreilly.servlet package. The Java Mail API provides both the basic and complex mail support; whereas you can implement only the basic mail support using the Mail Message class.

Spring Mail API

In the Spring Mail API hierarchy the org.springframework.mail package is the root level package for the spring framework’s email support. Inside the org.springframework.mail package, the MailSender interface is the central interface for sending e-mails. The MailSender interface supports only the plain text e-mails. The JavaMailSender interface of the org.springframework.mail.javamail package extends the MailSender interface and provides the methods for constructing and sending Multipurpose Internet Mail Extension (MIME) messages.

The following are the important classes and interfaces of the Spring Mail API.

clip_image001 The MailSender interface and SimpleMailMessage class.

clip_image001[1] The JavaMailSender and MimeMessagePreparator class.

clip_image001[2] The MimeMessageHelper class.

In this article you will come to know Mail support in the Spring framework and how we can send a plain text mail using MailSender interface.

Developing Mail Application in Spring

Now it’s time to develop basic e-mail application using the Spring Mail API. We are going to develop the basic mail application which creates simple e-mail containing text only. The name of the application developed here is SpringMailApp. The SpringMailApp application sends e-mails declaratively that is with the help of mappings declared in the mail declaration XML file. It contains few java files and a mail declaration XML file. Let’s create these Java and XML files next.


It is an interface and contains a method which is used to send the mail. Here is the code

Package com.app;
Public interface Birthday {
void sendMail();

Birthday interface has the sendMail() method which is implemented later in the BirthdayMail.java. Here is the code for BirthdayMail.java

Package com.app;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BirthdayMail implements Birthday
private MailSender MailSender;
private SimpleMailMessage templateMessage;
public void setMailSender(MailSender mailSender)
Public void setTemplateMessage(SimpleMailMessage templateMessge)
this. templateMessge= templateMessge;
Public void sendMail()
SimpleMailMessage msg= new SimpleMailMessage(this. templateMessge);
msg.setText(“Happy BirthDay to Chintu”);
System.out.println(“BirthDay Mail Sent Successfully”);
Catch (MailException ex)
Public static void main(String args[]) throws Exception
ApplicationContext act=new ClassPathXmlApplicationContext(“birthdayBeans.xml”);
BirthdayMail obj=(BirthdayMail) acx.getBean(“birthdayManager”);

After describing the java files now it’s time to discuss about the heart of the application i.e configuration(XML) file.

It contains declaration of the mail sender and the other beans used in the SpringMailApp application. It sets the host of the e-mail sender ,username and the password of the e-mail account from which the e-mail has to be sent.

Here the code for birthdayBeans.xml file

<? Xml version ="1.0" encoding ="UTF-8"?>
<beans xmlns="http://www.springframework.org/schemas/beans">
<bean id ="mailSender"
<Property name="host" value="smtpout.secureserver.net"/>
<Property name ="username" value="vinay.web@gmail.com"/>
<property name-="javaMailProperties" >
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<bean id =" templateMessage" Class ="org.springframework.mail.SimpleMailMessage">
<property name ="from" value="vinay.web@gmail.com"/>
<property name ="subject" value="birthDayWishes"/>
<bean id =" birthdayManager "
Class ="com.app.BirthdayMail">
<property name = "mailSender" ref="mailSender"/>
<property name ="templateMessge" ref="templateMessage"/>

Code Structure


  • Classes

    • com

      • app

        • Birthday.class

        • BirthdayMail.class

  • lib

    • jar files should be present here.

  • Src

    • com

      • app

        • Birthday.java

        • BirthdayMail.java

        • birthdayBeans.xml



Text Widget

Copyright © Vinay's Blog | Powered by Blogger

Design by | Blogger Theme by