Sending Gmail from Spring

Here’s a working example of how to send an Email (customised for the Gmail client) from a Spring application.

The POM would look like this:

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Few things to notice on this POM:

  • Specifying UTF-8 platform agnostic source encoding. If this property is not there Maven would complain about system specific encoding.
  • Due to the dependency tree, specifying spring-context is enough to bring the minimal number of Spring related jars that are needed to construct the minimal Spring project that uses the ApplicationContext, therefore Maven saves us from this trouble. Namely, the jars that are absolutely necessary:
    • spring-context
    • spring-beans
    • spring-core
    • spring-expression
    • spring-asm
    • commons-logging
  • Since we would be using Spring’s SimpleMailMessage the jar spring-support is also needed.
  • Also the javax.mail is needed for the despatching of the email.
  • Explicitly specifying java 1.6 for compilation using the maven maven-compiler-plugin plugin.

Our service layer is represented from the following class:

package com.dimitrisli.springEmail;

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

public class EmailService {

	private MailSender emailSender;
	private SimpleMailMessage exceptionEmail;

	public void sendEmail(String from, String to, String subj, String body){
		SimpleMailMessage message = new SimpleMailMessage();

	public void sendExceptionEmail(String error){
		SimpleMailMessage exceptionEmailMessage = new SimpleMailMessage(exceptionEmail);

Few things to notice:

  • @Service Spring annotation is another way of saying @Component but has a better representation of the service layer. Also specifying the bean id at that stage in the java bean makes the bean reference in an xml  configuration file unnecessary.
  • @Autowired is taking care of injecting the dependencies of the emailSender and exceptionEmail functionality. Loose coupling at its best.

Under /src/main/resources we store the Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
	<!--context namespace and schema for the component-scan to work-->
	<context:component-scan base-package="com.dimitrisli.springEmail" />

	<bean id="emailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host" value=""/>
		<property name="port" value="25"/>
		<property name="username" value=""/>
		<property name="password" value="yourpass"/>
		<property name="javaMailProperties">
				<prop key="mail.transport.protocol">smtp</prop>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="mail.smtp.starttls.enable">true</prop>
				<prop key="mail.debug">true</prop>

	<bean id="exceptionEmail" class="org.springframework.mail.SimpleMailMessage">
		<property name="from">
		<property name="to">
		<property name="subject" value="Something went wrong please investigate"/>


Things to notice:

  • Namespaces include the beans reference due to the tags we are using and the context reference due to the component-scan
  • component-scan on the specified package is activating the @Autowired magic.
  • The emailSender bean is tailored for the Gmail specific settings passing all the needed parameters to the JavaMailSenderImpl.
  • The exceptionEmail bean is following another technique having a preconfigured SimpleMailMessage.

Finally the MainSpringContainer which is laying down the two test scenarios creating a Spring Application Context:

package com.dimitrisli.springEmail;

import org.springframework.context.ApplicationContext;

public class MainSpringContainer {

	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/spring.xml");
		EmailService emailService = (EmailService) context.getBean("emailService");
		emailService.sendEmail("", "", "test subject", "test body");
		emailService.sendExceptionEmail("something went terribly wrong");

You can find the code in this Github repo


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s