trainingtrains Logo

91-9990449935

 0120-4256464

Spring Java Mail Tutorial

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:

spring with javamail api hierarchy
  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.

  1. MailMail.java
  2. applicationContext.xml
  3. Test.java

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.


1) MailMail.java

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.


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.


3) Test.java

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

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 Test.java file.
  • Compile and Run the Test class

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 MailMail.java and Test class will be same. You need to change only Sender email id in the Test.java file. Some changes are required in the applicationContext.xml file.

In the applicationContext.xml file, we are using:

  • mail.unitedsquaad.com for the host name. Change it.
  • a@unitedsquaad.com for the username. Change it.
  • xxxxx for the password. Change it.

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.


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.

The applicationContext.xml and Test.java 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).

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