Sending email with Spring Boot

Hello everyone, today we will learn how to send an email via SMTP in Spring Boot.
Technologies used: 
  • Spring Boot 2.1.1.RELEASE 
  • Java Mail 1.6.2
  • Maven 3 
  • Java 8
Project Structure

Maven
 

spring-boot-starter-mail will pull the JavaMail dependencies.
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.sibin.dev.emaildemo</groupId>
<artifactId>emailDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>emailDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Spring Boot Mail Server Properties

Once the dependency is in place, the next step is to specify the mail server properties in the application.properties file using the spring.mail.* namespace.

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<gmail username>
spring.mail.password=<gmail password>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true


Sending Email


Spring provides a JavaMailSender interface on top of JavaMail APIs.

Sending plain text email


package com.knf.sibin.dev.emaildemo.util;

import java.io.IOException;
import javax.mail.MessagingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

@Component
public class EmailUtil {

@Autowired
private JavaMailSender javaMailSender;
private static final Logger LOG = LoggerFactory.getLogger(EmailUtil.class);

public void sendEmail() throws MessagingException, IOException {

LOG.info("Ready to sent an email");
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("knowledgefactory4upeoples@gmail.com");
msg.setSubject("Test Mail");
msg.setText("Greetings from Spring Boot Email");
javaMailSender.send(msg);
LOG.info("Successfully dropped an email");

}

}


Demo


Start Spring Boot, and it will send out an email.

package com.knf.sibin.dev.emaildemo;

import java.io.IOException;
import javax.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.knf.sibin.dev.emaildemo.util.EmailUtil;

@SpringBootApplication
public class EmailDemoApplication implements CommandLineRunner {
@Autowired
private EmailUtil emailUtil;

public static void main(String[] args) {
SpringApplication.run(EmailDemoApplication.class, args);
}

@Override
public void run(String... args) {
try {
emailUtil.sendEmail();
} catch (MessagingException | IOException e) {
e.printStackTrace();
}
}
}



Terminal

. ____ _ __ _ _

/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \

( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \

\\/ ___)| |_)| | | | | || (_| | ) ) ) )

' |____| .__|_| |_|_| |_\__, | / / / /

=========|_|==============|___/=/_/_/_/

 Ready to sent an email

 Successfully dropped an email


Download Source Code



Run: mvn spring-boot:run


by sibin

web solution consultant for startups

+91-8075866854

happy coding

Comments