Spring – Sending e-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.

1. Project dependency

Add the JavaMail and Spring’s dependency.

File : pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mkyong.common</groupId>
  <artifactId>SpringExample</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringExample</name>
  <url>http://maven.apache.org</url>
  
  <repositories>
  	<repository>
  		<id>Java.Net</id>
  		<url>http://download.java.net/maven/2/</url>
  	</repository>
  </repositories>
  
  <dependencies>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    
    <!-- Java Mail API -->
    <dependency>
	    <groupId>javax.mail</groupId>
	    <artifactId>mail</artifactId>
	    <version>1.4.3</version>
    </dependency>
    
    <!-- Spring framework -->
    <dependency>
     	<groupId>org.springframework</groupId>
	    <artifactId>spring</artifactId>
	    <version>2.5.6</version>
    </dependency>
    
  </dependencies>
</project>

2. 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.

File : MailMail.java


package com.mkyong.common;

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 MailMail
{
	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:\\log.txt");
		helper.addAttachment(file.getFilename(), file);

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

3. Bean configuration file

Configure the mailSender bean, email template and specify the email details for the Gmail SMTP server.

File : Spring-Mail.xml


<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="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
	<property name="host" value="smtp.gmail.com" />
	<property name="port" value="587" />
	<property name="username" value="username" />
	<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="mailMail" class="com.mkyong.common.MailMail">
	<property name="mailSender" ref="mailSender" />
	<property name="simpleMailMessage" ref="customeMailMessage" />
</bean>
	
<bean id="customeMailMessage"
	class="org.springframework.mail.SimpleMailMessage">

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

</beans>

4. Run it


package com.mkyong.common;

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

public class App 
{
    public static void main( String[] args )
    {
    	ApplicationContext context = 
            new ClassPathXmlApplicationContext("Spring-Mail.xml");
    	 
    	MailMail mm = (MailMail) context.getBean("mailMail");
        mm.sendMail("Yong Mook Kim", "This is text content");
        
    }
}

output


 Dear Yong Mook Kim,
 Mail Content : This is text content
 
 Attachment : log.txt

Download Source Code

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
24 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
24 Comment authors
Mohammed HuzaifGurvinderPrabhat SwamiAbdennour ToumiHemant singh Recent comment authors
newest oldest most voted
Prabhat Swami
Guest
Prabhat Swami

this says: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target.

Gurvinder
Guest
Gurvinder

How do you scan an attachment before uploading? my requirement is to allow only speci extension types and also to amke sure its virus free

Mohammed Huzaif
Guest
Mohammed Huzaif

Plz try to make use of MimeTypes to allow only the specific extensions . And not sure about virus free. You would probably go for it if ur charging excess for it xD

Hemant singh
Guest
Hemant singh

Hi Mkyoung,
Thanks for the nice POST .This is very useful to me.
I need one more thing in the above code.
i.e. how to send mail to multiple users using the same code?.can u share this as well.

regards,
Hemant Singh

Mohammed Huzaif
Guest
Mohammed Huzaif

dude , Seriously . you just have to take multiple inputs in “toMail” variable by seperating it with a ” ; “

anugantigowtham
Guest
anugantigowtham

hi,

can you please provide me a example to send a mail along with a link to access the same application using spring mvc?

Thanks,

Gowtham. A

Sharath
Guest
Sharath

Thank You mkyong. Can you please specify how to not hard code the file path and keep it relative and place the file within the web application path

trackback
Spring???? | qiyadeng
Chandra
Guest
Chandra

Pls help me on this issue when take this article and execute. It is behaving so differently some time protocal issues some time data type issues. Here am getting exception recently below snippet. Thanks in advance..ASAP help me org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary=”—-=_Part_0_1221956599.1384766127208″; message exception details (1) are: Failed message 1: javax.mail.MessagingException: IOException while sending message; nested exception is: javax.activation.UnsupportedDataTypeException: no object DCH for MIME type multipart/mixed; boundary=”—-=_Part_0_1221956599.1384766127208″ at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1177) at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:416) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336) at com.brzee.controller.MailUtil.sendEmailNotify(MailUtil.java:64) at com.brzee.controller.HelloWorldController.helloWorld(HelloWorldController.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown… Read more »

Akshay
Guest
Akshay

Can you please tell me how to send email using JavaMailSender to a DistributionList or DL in outlook

Narasim
Guest
Narasim

Hi mkyong,

Thanks for the article. its very helpful.

I am having problem running this example, getting below error. Appreciate if you could help on this.

Thanks in Advance.

Exception in thread “main” java.lang.NullPointerException
at com.pp.api.email.MailMail.sendMail(MailMail.java:26)
at com.pp.api.email.MailMail.main(MailMail.java:49)

Thanks,
Narasim

Rimma
Guest
Rimma

Hi Mkyoung, thank you for your tutorial. But need your help!

I have a problem: I am using Spring MVC but without Maven.
I added sing-2.5.6.jar to project’s Java Build Path
I added to dispatcher-servlet.xml exactly code from your Spring-Mail.xml
and getting error:
SEVERE: Allocate exception for servlet dispatcher
java.lang.ClassNotFoundException: org.springframework.mail.javamail.JavaMailSenderImpl

Thank you,
Rimma

Simon Lawrence
Guest
Simon Lawrence

Great post, added my understanding massively, I’m now sending e-mails with attachments. Thanks.

muni krishna
Guest
muni krishna

12/08/13 17:41:23 INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a0c10f: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1a0c10f]; startup date [Mon Aug 13 17:41:23 IST 2012]; root of context hierarchy 12/08/13 17:41:23 INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [Spring-Mail.xml] 12/08/13 17:41:23 INFO support.ClassPathXmlApplicationContext: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a0c10f]: org.springframework.beans.factory.support.DefaultListableBeanFactory@86fe26 12/08/13 17:41:24 INFO support.DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@86fe26: defining beans [mailSender,mailMail,customeMailMessage]; root of factory hierarchy Exception in thread “main” org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 25; nested exception is: java.net.ConnectException: Connection timed out: connect at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:419) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:342) at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:338) at com.mkyong.common.MailMail.sendMail(MailMail.java:43) at com.mkyong.common.App.main(App.java:13)… Read more »

Charu
Guest
Charu

Can you anyone know how to send the email In HTML format? If so, kindly reply me.

Rubens Mariuzzo
Guest
Rahul
Guest
Rahul

Please reply, how to attache multiple files?

Bharatkumar
Guest
Bharatkumar

Hi Mkyoung, Thanks for very Nice Tutorial.
could you please tell, how to attach multiple files ?

Thanks in advance,

Bharatkumar

Kinjan Ajudiya
Guest
Kinjan Ajudiya

Hii Yong,

I have sent email with attachment.
Now i want to attach multiple files.
so what changes i should do in previous code ?

Thanks and Regards
Kinjan Ajudiya

Kinjan Ajudiya
Guest
Kinjan Ajudiya

Hello,
My name is Kinjan Ajudiya.
I am software Engineer.
I tried hard to find code for attachment with email.But at last i found from this post .Thank you very much..

Thanks and Regards
Kinjan Ajudiya

Zakos
Guest
Zakos

Hi , I get this error

EVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.NoClassDefFoundError: javax/mail/MessagingException
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getDeclaredConstructors(Class.java:1836)

Jitendra
Guest
Jitendra

Hi Yong,

In case of multiple attachment what changes i need to do.

Thanks
Jitendra

Vinidog
Guest
Vinidog

Hi Yong,

how to send in HTML Format?

Tks;

trackback
Spring Tutorials

[…] Sending e-mail with attachment Example to use Spring to send an email that has attachments. […]

Ranganath
Guest
Ranganath

Good Article. It really helped me get the clarity on the configuration part.

Rudi
Guest
Rudi

Hi,

Thanks for the article. Very informative.

I’m having a problem running the example here in
http://www.mkyong.com/spring/spring-sending-e-mail-with-attachment/

The problem is org.springframework.beans.factory.BeanCreationException: error creating bean with name mailMail defined in class path Spring-Mail.xml. Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property ‘simpleMailMessage’ of class MailMail. No property found.

However, simpleMailMessage is there just as the code shows.

Any help is appreciated. Thanks in advance.

Have a great day.

Best regards,

Rudi

trackback
??????????????? | ????????

[…] Spring – Sending e-mail with attachment | Spring […]