SMTP: Spring Mail To Program

Share this on .. Tweet about this on TwitterShare on LinkedIn0Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

All Java developers know, or should know, the Spring framework contains a lot of gems. Recently I discovered a new gem: an easy way to send an email. With the use of one single POJO mail class, you can easily let your application send an email and wire/inject al the mail configuration with the Spring applicationContext.xml.

Let’s first look at the simple POJO mail class.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;

public class MailMessage {

    private MailSender mailSender;

    private final SimpleMailMessage message = new SimpleMailMessage();

    public MailMessage(final String from) {

    public MailMessage setReply(final String replyTo) {
        return this;

    public MailMessage setTo(final String to) {
        return this;

    public MailMessage setCc(final String cc) {
        return this;

    public MailMessage setBcc(final String bcc) {
        return this;

    public MailMessage setSubject(final String subject) {
        return this;

    public MailMessage setBody(final String body) {
        return this;

    public void send() {

It only contains setters for the mail properties and one method, the send method.
All the setters are delegated (composition pattern [1]) to the SimpleMailMessage class provided by the Spring framework and return the current instance implementing the builder pattern [2].
The send method sends the real mail using the MailSender which is also part of the Spring framework.

This results in nice clean code when using it:

MailMessage message = new MailMessage("").setTo("");
message.setSubject("Hello").setBody("Hello World!").send();

The only thing to do is wiring the mail configuration (smtp settings) to the Spring MailSender class in the Spring applicationContext.xml. I’ve chosen to read them from a separate properties file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" 
             xsi:schemaLocation=""              xmlns:context="" 
             xmlns:xsi="" >

  <context:annotation-config />
  <context:spring-configured />
  <context:load-time-weaver />
  <context:component-scan base-package="" />

  <context:property-placeholder location="file:${oracle.j2ee.home}/config/" />

  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host">
    <property name="port">
    <property name="username">
    <property name="password">
    <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">${mail.smtp.auth}</prop>
        <!-- Use TLS to encrypt communication with SMTP server -->
        <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
        <prop key="mail.debug">${mail.debug}</prop>

So part of my properties file looks like this:

# Hostname of mail server ABC001
# Port nr of mail server 25
# Username to connect to mailserver (can be empty for anonymous connection)
# Password to connect to mailserver (can be empty for anonymous connection)
# Switch authorisation on (true) or off (false)
mail.smtp.auth= false
# Switch TLS encryption on (true) or off (false)
mail.smtp.starttls.enable = false
# Switch debug logging of spring mailcomponent on (true) or off (false)
mail.debug= true

Note that using the @Configurable annotation, Spring loadtime waving is used. This is needed to be able to inject in a POJO at creation with AspectJ. This means that the Spring loadtime waving jars have to be added besides the normal Spring library jars:

  • org.aspectj.aspectjweaver-x.y.z.jar
  • org.aspectj.aspectjrt-x.y.z.jar
  • org.springframework.spring-aspects-x.y.z.jar

Don’t forget to include the loadtime waving configuration tags in the Spring applicationContext.xml where the component-scan tag defines which packages have to be scanned for loadtime waving (you don’t want to scan all your jar libraries).

[1] Item 16: Favor composition over inheritance – Effective Java 2nd Edition – Joshua Bloch
[2] Item 2: Consider a builder when faced with many constructor parameters – Effective Java 2nd Edition – Joshua Bloch


Share this on .. Tweet about this on TwitterShare on LinkedIn0Share on Facebook0Share on Google+0Email this to someoneShare on Tumblr0Buffer this page

About Author

Emiel is a senior Java & integration consultant at AMIS, Nieuwegein (The Netherlands).

Comments are closed.