Sending E-mail using Spring Boot REST API

Mukul Jaiswal
cornercode
Published in
4 min readJul 27, 2018
Let’s get Started

This tutorial is about sending E-mail using REST API developed via Spring boot. Gmail SMTP server is always a good choice to check Mail REST API.

Spring framework provides a number of library classes for sending email. It provides an easy to use mail interface called JavaMailSender built on top of the JavaMail API. JavaMail API is a messaging framework available as part of Java SE and Java EE platforms.

Technologies Stack Required

  • Spring Tool Suite (STS)
  • Spring Boot
  • REST APIs
  • Maven
  • Java 8

Let’s create a Project

File => New => Spring Starter Project
Project Making in STS

Make sure you select Mail and Web dependencies for this project shown in right image above.

Spring-boot-starter-mail

The Spring tool suite will download all the dependencies essential for making this project.

Incase of IDEs other than Spring Tool Suite (STS)

You have to visit https://start.spring.io and download the zip folder, extract and Then- 👇

File => Import =>Import As existing maven project

Final project structure will look similar to :

Spring Project Structure

Packages and Classes made in this project:

Final Structure

Three Classes are made :

  • User.java — This class contains the E-mail of the user on which mail is to be send.
  • RegistrationController.java — This class contains an API for Sending mail with attachment as well as without attachment
  • MailService.java — This class contains two functions for E-mail sending. One is of Simple Mail and other is of mail that contains an attachment.

Let’s Begin Coding Part 🙌

pom.xml file

Pom.xml file contains all the dependencies for Spring starter project

spring-boot-starter-mail  is the important dependency that contains all the Java Mail Classes.

If you are using other IDE , then make sure following dependencies must be present before starting this project.

application.properties

In this, the configuration for this project is written.

Make sure less secure App is enabled for your E-mail id that you are going to use.To enable less secure App Click here !
Also Two-step verification should not be enabled for you E-mail Id.

I've written **** in place of username and password,that of course I'm not going to show you.Change a username as your <E-mail id> & password as your <password>

User.java

This class contains E-mail id of the user on which mail is to be send.

MailService.java

This class contains two function sendEmail() and sendEmailWithAttachment().

Actually ,Java provides a two classes for sending mail.If you want to send simple mail without attachment then object of SimpleMailMessage is used otherwise an Object of MimeMessage is used.

I have shown the implementation of both the classes.

Although using only MimeMessage class would works perfectly for both types of E-mails.

ClassPathResource Class is used to attach a file to the Java Program. Make sure you add an attachment file to src ->resources. That file will be sent as an attachment to the respective e-mail id.

RegistrationController.java

This controller class contains an API for sending mail. First API is developed for sending mail without attachment and the second API is for sending mail along with the attachment.

Make sure you write E-mail ID of the receiver in user.setEmailAddress(“your_email_id”); . I have written my email id above for an instance.

The APIs are accessible at URL :

  • For Simple Mail without attachment.
http://localhost:8080/send-mail
  • For Mail that contains an attachment.
http://localhost:8080/send-mail-attachment

Working Video

Find the source code of this project on my Github Repository attached below

Make project live on your System by:

Download or clone -> Import project ->Run As Spring Boot Application

If you enjoyed this post, please feel free to 👏 clap 👏, give my blog a
👣 follow 👣 and share with your friends.

You can find links to my social media below …

mukul.jaiswal786@gmail.com

Take Care & Happy Coding !

--

--