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;
 
@Service("mailService")
public class MailService {
    
    @Autowired
    private MailSender mailSender;
    @Autowired
    private SimpleMailMessage alertMailMessage;
    
    public void sendMail(String from, String to, String subject, String body) {

  

  SimpleMailMessage message = new SimpleMailMessage();

   

  message.setFrom(from);

  message.setTo(to);

  message.setSubject(subject);

  message.setText(body);

  mailSender.send(message);

  
    }
    
    public void sendAlertMail(String alert) {

  

  SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);

  mailMessage.setText(alert);

  mailSender.send(mailMessage);

  
    }
    
}


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

<?xml version="1.0" encoding="UTF-8"?>
 
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="


http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd


http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd


http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd


http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
>
 
    <context:component-scan base-package="com.javacodegeeks.spring.mail" />    
    
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">

  <property name="host" value="smtp.gmail.com"/>

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

  <property name="username" value="myusername@gmail.com"/>

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

  <property name="javaMailProperties">


<props>


    <!-- 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>


</props>

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

  <property name="from">




<value>myusername@gmail.com</value>

  </property>

  <property name="to">




<value>myusername@gmail.com</value>

  </property>

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

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

host=smtp.gmail.com
port=25
username=your-gmail-username
password=your-gmail-password
mail.transport.protocol=smtp
mail.smtp.auth=true
mail.smtp.starttls.enable=true


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;
import org.springframework.context.support.FileSystemXmlApplicationContext;
 
public class MailServiceTest {
 
    public static void main(String[] args) {

  

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

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

  

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

  

  mailService.sendAlertMail("Exception occurred");

  
    }
    
}

Related Article:

Related Whitepaper:

Java Essential Training

Author David Gassner explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more!

The course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts. Exercise files are included with the course.

Get it Now!  

Examples Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
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.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books