This tutorial will show you how to send a basic mail via Spring framework’s email support.

The Spring Framework provides a helpful utility library for sending email that shields the user from the specifics of the underlying mailing system and is responsible for low level resource handling on behalf of the client. The central interface for sending emails is the MailSender interface; a simple value object encapsulating the properties of a simple mail such as from and to (plus many others) is the SimpleMailMessage class. This package also contains a hierarchy of checked exceptions which provide a higher level of abstraction over the lower level mail system exceptions with the root exception being MailException.
For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application
Eclipse Mars
JDK 1.8
Have maven installed and configured
Spring and Java Mail dependencies in pom.xml
Now we will see the below steps how to create a maven based spring project in Eclipse
Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : springcore
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
        <!-- Spring framework -->
        <!-- Java Mail API -->
        <!-- junit -->

Step 3. If you see JRE System Library[J2SE-1.5] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.5], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Step 5. Create an XML file email.xml under src/main/resources.

Do right-click on src/main/resources in the project and go New -> file. Give File name: as email.xml and click on Finish button.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:p=""
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="" />
        <property name="port" value="121" />
        <property name="username" value="[email protected]" />
        <property name="password" value="******" />
        <property name="javaMailProperties">
                <!-- if you need ssl then uncomment below two lines -->
                <!-- <prop key=""></prop>
                <prop key="mail.smtp.starttls.enable">true</prop> -->
                <prop key="mail.smtp.auth">true</prop>
    <!-- this is a template message that we can pre-load with default state -->
    <bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage">
        <property name="from" value="[email protected]" />
    <bean id="emailSender" class="">
        <property name="mailSender" ref="mailSender" />
        <property name="templateMessage" ref="templateMessage" />

Step 6. Create below EmailSender class under src/main/java folder

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
public class EmailSender {
    private MailSender mailSender;
    private SimpleMailMessage templateMessage;
    public void sendEmail(final String subject, final String message, final String[] emailAddresses) {
        SimpleMailMessage mailMessage = new SimpleMailMessage(templateMessage);
        try {
            System.out.println("Email sending complete.");
        } catch (Exception e) {
    public MailSender getMailSender() {
        return mailSender;
    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    public SimpleMailMessage getTemplateMessage() {
        return templateMessage;
    public void setTemplateMessage(SimpleMailMessage templateMessage) {
        this.templateMessage = templateMessage;

Step 7. Create below Junit test class under src/test/java folder to test the application

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
public class EmailSenderTest {
    private EmailSender emailSender;
    public void setUp() throws Exception {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("email.xml");
        emailSender = applicationContext.getBean("emailSender", EmailSender.class);
    public void testSendEmail() {
        final String[] addresses = new String[] { "[email protected]" };
        emailSender.sendEmail("How are you ?", "Hello Dear, How are you ?", addresses);

Step 8. Now run the above Junit test class, you will see the below output in console

Console Output

Apr 14, 2016 1:10:16 PM prepareRefresh
INFO: Refreshing org[email protected]265ba4: startup date [Thu Apr 14 13:10:16 IST 2016]; root of context hierarchy
Apr 14, 2016 1:10:16 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [email.xml]
Email sending complete.

Step 9. Check the inbox you will get a message. If you do not find the message in inbox then check the Spam folder.
send email via spring
Thanks for reading.