Over a million developers have joined DZone.

Integration of Spring With Java Mail

DZone's Guide to

Integration of Spring With Java Mail

Look at how to integrate Spring with Java Mail. See an example of how to configure mail and send/receive mail using the Gmail server with Eclipse IDE in place.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Spring Java Mail

Integration of Spring With Java Mail

In this Spring Java Mail article, you will learn about another Spring integration with Java Mail. This article will allow you to send and receive emails using the Spring Framework interfaces and classes. You will be using Java Mail API for sending and receiving the mail. For more understanding, you will see a working example with Eclipse IDE in place.

Java Mail API

Spring Framework has many user interfaces and classes for sending/receiving your emails. There is a package called org.springframework.mail. It is a root package which provides the mail support in the Spring Framework.

The classes and interfaces required for java mail support in Spring Framework are defined as follows:

Spring Java Mail

MailSender Interface

It is a root interface that provides basic functionality for sending the simple mail.

JavaMailSender Interface

It is sub-interface of MailSender. It supports MIME messages and is used with MimeMessageHelper class. It is used for creating JavaMail MimeMessage. MimeMrssagePreparator mechanism is recommended for the use of this interface.

JavaMailSenderImpl Class

It has the implementation of the JavaMailSender interface and supports JavaMail Mime Messages and the Spring Simple Mail Messages.

SimpleMailMessage Class

It provides help in creating a simple mail message including from, to, cc, subject, etc.

MimeMessagePreparator Interface

It is used as a call back interface for preparing the JavaMail MIME messages.

MimeMessageHelper Class

It is used as a helper class, which contains MIME message and offers support for inline elements like HTML text content, images etc.

Example of Sending Mail in Spring Using Gmail Server

In this example, you will see two Spring Mail classes:

  • SimpleMailMessage for creating messages.
  • JavaMailSenderImple for sending the messages.

The following files are needed for sending the email using Spring Framework:

  • MailMail.java
  • ApplicationContext.java
  • Test.java

Spring Java Mail

Before you start, you need to have mail.jar and activation.jar for running this example.


package com.example;  
import org.springframework.mail.MailSender;  
import org.springframework.mail.SimpleMailMessage;  
public class MailMail{  
    private MailSender mailSender;   
    public void setMailSender(MailSender mailSender) {  
        this.mailSender = mailSender;  
    public void sendMail(String from, String to, String subject, String msg) {             SimpleMailMessage message = new SimpleMailMessage();  

It is the simple class that is used for defining mailSender property, and the object of MailSender will be provided to this property during the runtime. The send() of MailSender interface is used for sending simple mail.


In this XML file, you will create a bean for JavaMailSenderImpl class. You need to define the values of the below properties:

  • Host
  • Username
  • Password
  • JavaMailProperties
<?xml version="1.0" encoding="UTF-8"?>  
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
  <property name="host" value="smtp.gmail.com" />  
    <property name="username" value="yourgmailid@gmail.com" />  
    <property name="password" value="yourgmailpassword" />  
    <property name="javaMailProperties">  
              <prop key="mail.smtp.auth">true</prop>  
              <prop key="mail.smtp.socketFactory.port">465</prop>  
              <prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>  
              <prop key="mail.smtp.port">465</prop>  
<bean id="mailMail" class="com.example.MailMail">  
    <property name="mailSender" ref="mailSender" />  

Spring Java Mail


This class gets the bean of email mail from the applicationContext.xml file and calls the sendmail method of MailMail class.

package com.example;  
import org.springframework.beans.factory.*;  
import org.springframework.beans.factory.xml.XmlBeanFactory;  
import org.springframework.core.io.*;  
public class Test {   
      public static void main(String[] args) {     
      Resource r=new ClassPathResource("applicationContext.xml");  
      BeanFactory b=new XmlBeanFactory(r);  
      MailMail m=(MailMail)b.getBean("mailMail");  
      String sender="sendergmailid@gmail.com";//write here sender gmail id  
      String receiver="receiveremailid@gmail.com";//write here receiver id  

To run the example:

  • Load the Spring jar files for java mail.
  • Load the jar files mail.jar and activation.jar
  • Change the properties such as username and password in applicationContext.xml file.
  • Change sender gmail id and receivemail id in Test.java file.
  • Compile and run the Test class.

So, this was all about Spring Java Mail. Hope you like our explanation.


In this session, you learned about the Spring integration with Mail API. You saw how Java Mail API is used for sending and receiving the mail. For more understanding, you saw a working example of how to configure mail and send/receive mail using the Gmail server with Eclipse IDE in place. If you have any questions, feel free to ask in the comments section.

Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

spring ,programming ,java ,java mail api ,integration ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}