Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Sending e-mails with Spring

We will create a simple Spring service that will be used for sending mail. One method will create a SimpleMailMessage on the spot, while the other will use a preconfigured default message. The source code for the service is the following:

package com.javacodegeeks.spring.mail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
public class MailService {
    private MailSender mailSender;
    private SimpleMailMessage alertMailMessage;
    public void sendMail(String from, String to, String subject, String body) {


  SimpleMailMessage message = new SimpleMailMessage();







    public void sendAlertMail(String alert) {


  SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);




The corresponding Spring XML file that bootstraps the container is the following:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <context:component-scan base-package="com.javacodegeeks.spring.mail" />    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

  <property name="host" value=""/>

  <property name="port" value="25"/>

  <property name="username" value=""/>

  <property name="password" value="mypassword"/>

  <property name="javaMailProperties">


    <!-- Use SMTP transport protocol -->

    <prop key="mail.transport.protocol">smtp</prop>

    <!-- Use SMTP-AUTH to authenticate to SMTP server -->

    <prop key="mail.smtp.auth">true</prop>

    <!-- Use TLS to encrypt communication with SMTP server -->

    <prop key="mail.smtp.starttls.enable">true</prop>

    <prop key="mail.debug">true</prop>


    <bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">

  <property name="from">



  <property name="to">



  <property name="subject" value="Alert - Exception occurred. Please investigate"/>

In case you wish to use Gmail’s SMTP server, make sure that the following JavaMail properties are configured appropriately:

Finally, we create a class that will be used to create the Spring container and test the simple mail service we created. The source code is the following:

package com.javacodegeeks.spring.mail;
import org.springframework.context.ApplicationContext;
public class MailServiceTest {
    public static void main(String[] args) {


  ApplicationContext context = new FileSystemXmlApplicationContext("conf/spring.xml");

  MailService mailService = (MailService) context.getBean("mailService");


  mailService.sendMail("", "", "Testing123", "Testing only nn Hello Spring Email Sender");


  mailService.sendAlertMail("Exception occurred");


Related Article:

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.
Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below: