Thursday, December 1, 2011

How to send mail using Spring and Java Mail Api

In this tutorial i will explain step by step how to integrate Spring with java mail.Spring provides easy configuration inside SpringContext xml file to enable integration with java mail. In this tutorial i have used myeclipse IDE for development.You can use any java development IDE of your choice.

Jar Required:
1)Java Mail jar(download from www.oracle.com)
2)Spring2.5 jar(www.springsource.org/download)

Steps:

1)Create a java project
2)Add Spring capabilities with the following libraries:
AOP Library
Core Library
Misc. Library
3)Add the mail jar files to project build path
activation.jar
dsn.jar
imap.jar
mail.jar
mailapi.jar
pop3.jar
smtp.jar

Component Required:
4)Write your own SMTPAuthenticator class to verify the username(mail id) and password.

GnbSMTPAutenticator.java

package com.gnb.spring.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

public class GnbSMTPAuthenticator extends Authenticator {
private String username;
private String password;
public GnbSMTPAuthenticator(String username, String password) {
super();
this.username = username;
this.password = password;
System.out.println("GnbSMTPAuthenticator invoked and finished");
System.out.println(username);
System.out.println(password);
}
public PasswordAuthentication getPasswordAuthentication()
{
System.out.println("Auth invoked...");
return new PasswordAuthentication(username,password);
}
}
5)Configure your GnbSMTPAuthenticator class in the spring context xml with the valid email id & password

SpringContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="smtpAuthenticator" class="com.gnb.spring.mail.GnbSMTPAuthenticator">
<constructor-arg>
<value>your mail id</value>
</constructor-arg>
<constructor-arg>
<value>***your password****</value>
</constructor-arg>
</bean>
<bean id="mailSession" class="javax.mail.Session" factory-method="getInstance">
<constructor-arg>
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.port">465</prop>
<prop key="mail.debug">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.socketFactory.fallback">false</prop>
</props>
</constructor-arg>
<constructor-arg ref="smtpAuthenticator"/>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

<property name="host" value="smtp.gmail.com"/>
<property name="session" ref="mailSession"/>
</bean>
<bean id="msgTemp" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="your mail id"/>
</bean>
<bean id="workShop" class="com.gnb.spring.mail.WorkshopMailService">
<property name="mailSender" ref="mailSender"/>
<property name="msgTemp" ref="msgTemp"/>
</bean>
</beans>Note: you should write your own mail id and password which will be validated

6)Configure the mailSession in SpringContext.xml with the following
a)mail properties
b)smtp authenticator

7)Configure mailSender in the SpringContext with the following
a)host
b)mailsession

8)Configure mailMessage in the SpringContext.xml

9)Identify the business Services from where you want to deliver the mail and inject the following.
a)mailsender
b)mailmessage

WorkshopMailService.java

package com.gnb.spring.mail;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

public class WorkshopMailService {
private JavaMailSender mailSender;
private SimpleMailMessage msgTemp;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
System.out.println("mailsender created..");
}
public void setMsgTemp(SimpleMailMessage msgTemp) {
this.msgTemp = msgTemp;
System.out.println("message Temp created..");
}
public void sendMail(String to,String sub,String body)
{
System.out.println("send mail invoked....");
msgTemp.setTo(to);
msgTemp.setSubject(sub);
msgTemp.setText(body);
mailSender.send(msgTemp);
System.out.println("send mail finished....");
}
public JavaMailSender getMailSender() {
return mailSender;
}
public SimpleMailMessage getMsgTemp() {
return msgTemp;
}
}

10) Write your Client code as follows and test


MailClient.java

package com.gnb.spring.mail;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MailClient {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
WorkshopMailService ws=(WorkshopMailService)ctx.getBean("workShop");
System.out.println(ws);
String to="ganesh.spring2@gmail.com";
String sub="About Life";
String body="Life is a rough paper,It is too difficult to understand but easy to destroy!!";
ws.sendMail(to, sub, body);

}

}

pls let me know if you have any doubt...my mail id:

ganesh.spring2@gmail.com

Cheers:))))Have a nice day you guys......