Send Email Using 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:


Dependency Managemen(pom.xml):

spring-boot-starter-mail will pull the JavaMail dependencies.
<?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.




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("knowledgefactory333@gmail.com");
msg.setSubject("Test Mail");
msg.setText("Greetings from Spring Boot Email");
javaMailSender.send(msg);
LOG.info("Successfully dropped an email");

}
}



Main class


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();
}
}
}




















Local Setup:

Step 1: Download or clone the source code to a local machine.

Step 2mvn clean install

Step 3: Run the Spring Boot application
mvn spring-boot:run


Terminal

. ____ _ __ _ _

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

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

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

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

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

 Ready to sent an email

 Successfully dropped an email


Download Source Code


Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete