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;

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();
forgotPasswordMav.setViewName(”forgot”);

if(request.getMethod().equalsIgnoreCase(”post”)){

// 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);
msg.setTo(email);
msg.setText(
“Dear user,”
+ “Your requested login information are following”
+ “Username :” + username
+ “Password :” + password
+ “, thank you for being with us ”
+ “Thanking,”
+ “Customer Care”);
try{
this.mailSender.send(msg);
status = 1;
}
catch(MailException ex) {
// simply log it and go on…
System.err.println(ex.getMessage());
}
}

return forgotPasswordMav;
}

}

Find below the bean definitions for the above code:

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

<!–
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=”youremail@yourdomain.com” />
<property name=”subject” value=”Your Login Information” />
</bean>

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

mail.jar
activation.jar

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

One Comment

Raj Identicon Icon Raj  on April 16th, 2010

Hi,
I am new to Spring.Worked on it in a small project and that was my first java development project.Was not involved much in configuration of spring in project.This post was of help for me to some overview though I didnt understand much of it:-(. Impressed with your website, you have worked on many technologies in short time I feel.Keep up the good work going!!By the way, how do you manager your time to work,study & posting these articles?

Leave a Comment