Pages

Saturday, July 31, 2010

Spring - Sending Mail with Attachment

Here’s an example to use Spring to send e-mail that has attachments via Gmail SMTP server. In order to contains the attachment in your e-mail, you have to use Spring’s JavaMailSender & MimeMessage , instead of MailSender & SimpleMailMessage.


Spring’s Mail Sender

You have to use JavaMailSender instead of MailSender to send attachments, and attach the resources with MimeMessageHelper. In this example, it will get the “c:\\log.txt” text file from your file system (FileSystemResource) as an e-mail attachment.

Beside file system, you can also get any resources from URL path(UrlResource), Classpath (ClassPathResource), InputStream (InputStreamResource)… please refer to Spring’s AbstractResource implemented classes.

To run the below example you need below jar files in your class path
  • mail-1.4.1.jar
  • spring-2.5.6.jar
  • spring-jms.jar

TestMail.java

package com;

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 TestMail
{
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) {
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("C:\\Users\\vardhan\\Desktop\\log.txt");
helper.addAttachment(file.getFilename(), file);
}catch (MessagingException e) {
throw new MailParseException(e);
}
mailSender.send(message);
}
}

applicationContext.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.0.xsd">
<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="username" />  <!-- Please give ur gmail username -->
<property name="password" value="password" />  <!-- Please give ur gmail 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="mailMail" class="com.TestMail">
<property name="mailSender" ref="mailSender" />
<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
<bean id="customeMailMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="yourMailId@gmail.com" />
<property name="to" value="yourMailId@gmail.com" />
<property name="subject" value="Testing Subject" />
<property name="text">
<value>
<![CDATA[
Dear %s,
%s
Please find the log file in the attachement.
]]>
</value>
</property>
</bean>

</beans>

Run it


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

public class Main 
{
    public static void main( String[] args )
    {
     ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml");
     TestMail mm = (TestMail) context.getBean("mailMail");
     System.out.println("Before Sending Message >>>>>>>>>>>>>>>>>");
        mm.sendMail("Vardhan", "Congrats, Succefully you have a sent a mail from Spring application.");
        System.out.println("Mail sucessfully sent to your mail bix,Please check >>>>>>>>>>>>>>>>>");
    }
}


Out put
Before Sending Message >>>>>>>>>>>>>>>>>
Mail sucessfully sent to your mail bix,Please check  >>>>>>>>>>>>>>>>>

No comments:

Post a Comment