Spring - JavaMail Integration Example

[Updated: Aug 5, 2018, Created: Aug 5, 2018]

Following example shows how to integrate JavaMail with Spring application.

Spring provides MailSender interface which hides the specifics of the underlying mailing system. JavaMailSender is a sub-interface which integrates with JavaMail; JavaMailSenderImpl is its implementation.


Maven dependencies




package com.logicbig.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;

public class AppConfig {
  private static final String SENDER_EMAIL = "";//change with your sender email

  public MailSender mailSender() {
      JavaMailSenderImpl mailSender = new JavaMailSenderImpl();

      Properties props = new Properties();
      props.put("mail.smtp.auth", "true");//Outgoing server requires authentication
      props.put("mail.smtp.starttls.enable", "true");//TLS must be activated

      mailSender.setPassword("12345678");//change with your sender email password
      mailSender.setHost(""); //Outgoing smtp server - change it to your SMTP server
      mailSender.setPort(587);//Outgoing port
      return mailSender;

  public SimpleMailMessage defaultMessage() {
      SimpleMailMessage smm = new SimpleMailMessage();
      smm.setSubject("Default subject");
      smm.setText("Default text");
      return smm;

SimpleMailMessage is an implementation of MailMessage which encapsulates email message details. In above example we registered an instance of SimpleMailMessage as bean which we are going to use as a template message to create final message.

Example Client

package com.logicbig.example;

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

public class ExampleClient {

  private MailSender mailSender;
  private SimpleMailMessage mailMessage;

  public void sendMail(){
      System.out.println("sending mail");
      SimpleMailMessage msg = new SimpleMailMessage(mailMessage);//using copy constructor
      msg.setSubject("test subject");
      msg.setText("spring email integration test");

Main class

public class SpringEmailExampleMain {

  public static void main(String[] args) {
      AnnotationConfigApplicationContext context =
              new AnnotationConfigApplicationContext(AppConfig.class);
      ExampleClient bean = context.getBean(ExampleClient.class);
sending mail

The receiver:

Example Project

Dependencies and Technologies Used:

  • spring-context-support 5.0.8.RELEASE: Spring Context Support.
  • javax.mail 1.6.1 JavaMail API
  • JDK 9
  • Maven 3.5.4

Spring JavaMail Integration Example. Select All Download
  • spring-java-mail-integration-example
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also