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
To learn more or other ways of sending mail from spring application you can visit the following link:
Click Here

I am a simple man working in IT field. I love Music, playing soccer, traveling new places. By profession I am a software engineer and enjoy my job. Currently I am doing my M Sc in Software Engineering in Blekinge Institute of Technology, Sweden. I love researching on new technologies such as programming languages, architectures, tools and techniques.
Recent Comments