Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages

Spring Java Mail

Spring framework provides many useful interfaces and classes for sending and receiving mails.

The org.springframework.mail package is the root package that provides mail support in Spring framework.

Spring Java Mail API

The interfaces and classes for java mail support in spring framework are as follows:


  1. MailSender interface: It is the root interface. It provides basic functionality for sending simple mails.
  2. JavaMailSender interface: It is the subinterface of MailSender. It supports MIME messages. It is mostly used with MimeMessageHelper class for the creation of JavaMail MimeMessage, with attachment etc. The spring framework recommends MimeMessagePreparator mechanism for using this interface.
  3. JavaMailSenderImpl class: It provides the implementation of JavaMailSender interface. It supports JavaMail MimeMessages and Spring SimpleMailMessages.
  4. SimpleMailMessage class: It is used to create a simple mail message including from, to, cc, subject and text messages.
  5. MimeMessagePreparator interface: It is the callback interface for the preparation of JavaMail MIME messages.
  6. MimeMessageHelper class: It is the helper class for creating a MIME message. It offers support for inline elements such as images, typical mail attachments and HTML text content.

Example of Sending mail in Spring by Gmail Server

In this example, we are using two spring mail classes:

  1. SimpleMailMessage for creating message.
  2. JavaMailSenderImpl for sending message.

You need to create following files for sending email through Spring framework.

  2. applicationContext.xml

You need to load mail.jar and activation.jar files to run this example.

download mail.jar and activation.jar or go to the Oracle site to download the latest version.


It is the simple class that defines mailSender property. An object of MailSender will be provided to this property at runtime.

In the sendMail() method, we are creating the instance of SimpleMailMessage and storing the information into this object such as from, to, subject and message.

The send() method of MailSender interface is used here to send the simple mail.

  1. package com.Kreationnext;
  2. import org.springframework.mail.MailSender;
  3. import org.springframework.mail.SimpleMailMessage;
  4. public class MailMail{
  5.     private MailSender mailSender;
  6.     public void setMailSender(MailSender mailSender) {
  7.         this.mailSender = mailSender;
  8.     }
  9.     public void sendMail(String from, String to, String subject, String msg) {
  10.         //creating message
  11.         SimpleMailMessage message = new SimpleMailMessage();
  12.         message.setFrom(from);
  13.         message.setTo(to);
  14.         message.setSubject(subject);
  15.         message.setText(msg);
  16.         //sending message
  17.         mailSender.send(message);
  18.     }
  19. }

2) applicationContext.xml

In this xml file, we are creating a bean for JavaMailSenderImpl class. We need to define values of following properties:

  1. host
  2. username
  3. password
  4. javaMailProperties

We are also creating the bean for MailMail class with mailSender property. Now, the instance of JavaMailSenderImpl class will be set in the mailSender property of MailMail class.

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“”
  4.     xmlns:xsi=“”
  5.     xmlns:p=“”
  6.     xsi:schemaLocation=” 
  8. <bean id=“mailSender” class=“org.springframework.mail.javamail.JavaMailSenderImpl”>
  9.   <property name=“host” value=“” />
  10.     <property name=“username” value=“” />
  11.     <property name=“password” value=“yourgmailpassword” />
  12.     <property name=“javaMailProperties”>
  13.        <props>
  14.               <prop key=“mail.smtp.auth”>true</prop>
  15.               <prop key=“mail.smtp.socketFactory.port”>465</prop>
  16.               <prop key=“mail.smtp.socketFactory.class”></prop>
  17.               <prop key=“mail.smtp.port”>465</prop>
  18.         </props>
  19.     </property>
  20. </bean>
  21. <bean id=“mailMail” class=“com.Kreationnext.MailMail”>
  22.     <property name=“mailSender” ref=“mailSender” />
  23. </bean>
  24. </beans>


This class gets the bean of mailMail from the applicationContext.xml file and calls the sendMail method of MailMail class.

  1. package com.Kreationnext;
  2. import org.springframework.beans.factory.*;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import*;
  5. public class Test {
  6. public static void main(String[] args) {
  7. Resource r=new ClassPathResource(“applicationContext.xml”);
  8. BeanFactory b=new XmlBeanFactory(r);
  9. MailMail m=(MailMail)b.getBean(“mailMail”);
  10. String sender=“”;//write here sender gmail id
  11. String receiver=“”;//write here receiver id
  12. m.sendMail(sender,receiver,“hi”,“welcome”);
  13. System.out.println(“success”);
  14. }
  15. }

How to run this example

  • Load the spring jar files for core and java mail
  • Load the mail.jar and activation.jar
  • Change the username and password property in the applicationContext.xml file, specifying your gmail id and password.
  • Change the sender gmail id and receivermail id in the file.
  • Compile and Run the Test class
download this example (developed using myeclipse IDE)

Example of Sending mail in Spring by Server provided by host provider

If you have your own site, you can use your mail server. The and Test class will be same. You need to change only Sender email id in the file. Some changes are required in the applicationContext.xml file.

In the applicationContext.xml file, we are using:

  • for the host name. Change it.
  • for the username. Change it.
  • xxxxx for the password. Change it.
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans
  3.     xmlns=“”
  4.     xmlns:xsi=“”
  5.     xmlns:p=“”
  6.     xsi:schemaLocation=” 
  8. <bean id=“mailSender” class=“org.springframework.mail.javamail.JavaMailSenderImpl”>
  9.   <property name=“host” value=“” />
  10.     <property name=“username” value=“” />
  11.     <property name=“password” value=“xxxxx” />
  12.     <property name=“javaMailProperties”>
  13.        <props>
  14.               <prop key=“mail.smtp.auth”>true</prop>
  15.            </props>
  16.     </property>
  17. </bean>
  18. <bean id=“mailMail” class=“MailMail”>
  19.     <property name=“mailSender” ref=“mailSender” />
  20. </bean>
  21. </beans>
download this example (developed using myeclipse IDE)

Sending mails to multiple receivers

You can send mails to multiple receivers by the help of SimpleMailMessage class. The setTo(String[] receivers) method of SimpleMailMessage class is used to send message to multiple receivers. Let’s see the simple code.

  1.      ….
  2. public void sendMail(String from, String[] to, String subject, String msg) {
  3.        //creating message
  4.     SimpleMailMessage message = new SimpleMailMessage();
  5.         message.setFrom(from);
  6.     message.setTo(to);//passing array of recipients
  7.     message.setSubject(subject);
  8.     message.setText(msg);
  9.        //sending message
  10.     mailSender.send(message);
  11. }
  12.     …

Spring MimeMessagePreparator Example

We can send the mime message by the help of MimeMessagePreparator interface. It has one method prepare(MimeMessage message).

Let’s see the simple code to send mime message.

  1.   import javax.mail.Message;
  2. import javax.mail.internet.InternetAddress;
  3. import javax.mail.internet.MimeMessage;
  4. import org.springframework.mail.javamail.JavaMailSender;
  5. import org.springframework.mail.javamail.MimeMessagePreparator;
  6. public class MailMail{
  7.     private JavaMailSender mailSender;
  8.     public void setMailSender(JavaMailSender mailSender) {
  9.         this.mailSender = mailSender;
  10.     }
  11.     public void sendMail(final String from, final String to,final String subject,final String msg) {
  12.         MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {
  13.                 public void prepare(MimeMessage mimeMessage) throws Exception {
  14.                    mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(to));
  15.                    mimeMessage.setFrom(new InternetAddress(from));
  16.                    mimeMessage.setSubject(subject);
  17.                    mimeMessage.setText(msg);
  18.                 }
  19.         };
  20.         mailSender.send(messagePreparator);
  21.     }
  22. }

The applicationContext.xml and files are same as given above.

Sending Attachment by Spring MimeMessageHelper Example

We can send the mime message with attachment in spring by the help of MimeMessageHelper class. It is recommended to use than MimeMessagePreparator.

Let’s see the simple code to send mime message with attachment(image).

  1. import;
  2. import javax.mail.MessagingException;
  3. import javax.mail.internet.MimeMessage;
  4. import;
  5. import org.springframework.mail.javamail.JavaMailSender;
  6. import org.springframework.mail.javamail.MimeMessageHelper;
  7. public class MailMail{
  8.     private JavaMailSender mailSender;
  9.     public void setMailSender(JavaMailSender mailSender) {
  10.         this.mailSender = mailSender;
  11.     }
  12.     public void sendMail(final String from, final String to,final String subject,final String msg) {
  13.         try{
  14.         MimeMessage message = mailSender.createMimeMessage();
  15.         MimeMessageHelper helper = new MimeMessageHelper(message, true);
  16.         helper.setFrom(from);
  17.         helper.setTo(to);
  18.         helper.setSubject(subject);
  19.         helper.setText(msg);
  20.         // attach the file
  21.         FileSystemResource file = new FileSystemResource(new File(“c:/rr.jpg”));
  22.         helper.addAttachment(“mybrothermage.jpg”, file);//image will be sent by this name
  23.         mailSender.send(message);
  24.         }catch(MessagingException e){e.printStackTrace();}
  25.     }
  26. }

The applicationContext.xml and files are same as given above.