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();
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

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

<pre><servlet>
  <servlet-name>dwr-invoker</servlet-name>
  <display-name>DWR Servlet</display-name>
  <servlet-class>
    org.directwebremoting.servlet.DwrServlet
  </servlet-class>
  <init-param>
     <param-name>debug</param-name>
     <param-value>true</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>dwr-invoker</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping></pre>

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

<pre><!DOCTYPE dwr PUBLIC
    "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
    "<a href="http://getahead.org/dwr/dwr20.dtd">http://getahead.org/dwr/dwr20.dtd</a>">

<dwr>
  <allow>
    <create creator="new" javascript="JDate">
      <param name="class" value="java.util.Date"/>
    </create>
    <create creator="new" javascript="Demo">
      <param name="class" value="your.java.Bean"/>
    </create>
  </allow>
</dwr></pre>

To check go to the following URL.

http://localhost:8080/[YOUR-WEBAPP]/dwr/

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