Monday, 19 March 2012

Spring email setting with attachment facility

Web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>ReportGeneration</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>

</web-app>

---------------------------------------------------------------------

springapp-servlet.xml

In the below settings, please change the username and password with your email ID and password. In the bean "customeMailMessage" please change the "from" and "to" property to your email ID and email ID in which the email has to be sent. Please note both the property can be dynamically set in the code as well.



<?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">

<!-- the application context definition for the springapp DispatcherServlet -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp"></property>
</bean>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="your@emailid.com" />
<property name="password" value="password" />

<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>

<bean id="eMail" class="com.lnt.infotech.EMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>

<bean id="customeMailMessage" class="org.springframework.mail.SimpleMailMessage">

<property name="from" value="your@emailid.com" />
<property name="to" value="reciever@emailid.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
Mail Content : %s
]]>
</value>
</property>
</bean>

</beans>

In the above bean "eMail" configuration, two property marked in background colour green is Dependency Injection where we are injecting the configuration values into the class com.lnt.infotech.EMail.



---------------------------------------------------------------------

SendEmail.java

package com.lnt.infotech;

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

public class SendEmail {

public static void main(String[] args) {
System.out.println("--------Start---------");
ApplicationContext context = new ClassPathXmlApplicationContext("springapp-servlet.xml");

EMail mm = (EMail) context.getBean("eMail");
mm.sendMail("Prajith", "This is text content");

}
}



In the above code we are just getting the object of class EMail.java from the WebApplicationContext.xml(spring-app.xml) and accessing the method sendMail.


----------------------------------------------------------------------

EMail.java

package com.lnt.infotech;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

public class EMail {
private JavaMailSender mailSender;
private SimpleMailMessage simpleMailMessage;

public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
this.simpleMailMessage = simpleMailMessage;
}

public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}

public void sendMail(String dear, String content) {
System.out.println("sendMail");
MimeMessage message = mailSender.createMimeMessage();

try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(simpleMailMessage.getTo());
helper.setSubject(simpleMailMessage.getSubject());
helper.setText(String.format(simpleMailMessage.getText(), dear,
content));

FileSystemResource file = new FileSystemResource("/Users/pkomalac/Downloads/unix.pdf");
helper.addAttachment(file.getFilename(), file);

} catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}



---------------------------------------------------------------------


Required jar

1. commons-logging.jar
2. jstl.jar
3. mail.jar
4. spring-webmvc.jar
5. spring.jar
6. standard.jar

---------------------------------------------------------------------

Project Structure



No comments:

Post a Comment