Sending Email From Your Spring Application

The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client.

In my last project I have used spring mail functionality to send mail to the user. This is the simplest way so far I have found to send mail from spring application.

Let us also assume that there is a requirement stating that an email message with login information needs to be sent to the user.

Basic MailSender and SimpleMailMessage usage

Sample Controller:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;</code>

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ForgotPasswordFormController implements Controller{
private MailSender mailSender;
private SimpleMailMessage simpleMailMessage;

public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws   Exception{
ModelAndView forgotPasswordMav = new ModelAndView();


// Do the business calculations...

// Create a thread safe "copy" of the template message and customize it

String email = request.getParameter("email");
SimpleMailMessage msg = new SimpleMailMessage(this.simpleMailMessage);
"Dear user,"
+ "Your requested login information are following"
+ "Username :" + username
+ "Password :" + password
+ ", thank you for being with us "
+ "Thanking,"
+ "Customer Care");
status = 1;
catch(MailException ex) {
// simply log it and go on...

return forgotPasswordMav;


Find below the bean definitions for the above code:

<bean name="/" class="com.mycompany.businessapp.ForgotPasswordFormController">
<property name="mailSender" ref="mailSender"></property>
<property name="simpleMailMessage" ref="simpleMailMessage"></property>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value=""/>

this is a template message that we can pre-load with default state
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="" />
<property name="subject" value="Your Login Information" />

To use this you will need the following two jars in your /WEB-INF/lib


To learn more or other ways of sending mail from spring application you can visit the following link:
Click Here

Read More

Itegrating DWR (Direct Web Remoting) with Spring

Hi Springers, I have used DWR (Direct Web Remoting) in my last project which was pretty interesting and I want to share the simplest way of using DWR with spring application. Download the latest version of dwr.jar from here. And put it in the /WEB-INF/lib folder. I have used DWR 2.0.5.

Add the following lines in your web.xml

  <display-name>DWR Servlet</display-name>


Now create a dwr.xml in the WEB-INF folder alongside web.xml and add the following line in that file.

    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "<a href=""></a>">

    <create creator="new" javascript="JDate">
      <param name="class" value="java.util.Date"/>
    <create creator="new" javascript="Demo">
      <param name="class" value=""/>

To check go to the following URL.


You should see a page showing you the classes that you’ve selected.

To learn detail about DWR please visit the documentation site of DWR.

Read More