EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring boot email
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

Spring boot email

Spring boot email

Introduction to Spring boot email

In our application, we may encounter a requirement where we have to send emails based on some conditions, for this spring boot provide us with an easy way to send them without making any changes to the existing code, we have to add the dependency provided by the spring boot framework at our classpath. This provides us lots of interface and classes that can be used to prepare the email and send to the user whenever required. We can send the email using the Gmail SMTP server, and they can send it by using the Transport layer security of Gmail. We have to make more configurations rather than just adding the dependency to the build file; in the coming section of the tutorial, we will see how we can configure this in our application and the internal working and implementation for better understating and clarity.

Syntax

As we have discussed, this is used to send the email to the user whenever it is required; for this, we have to make changes in the application property file; let’s take a closer look at the syntax for better understanding; see below;

spring.mail.host= host name

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

spring.mail.port= port number

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,754 ratings)

spring.mail.username= user for smtp server

spring.mail.password= password for smtp server

spring.mail.properties.mail.smtp.auth= true or false

spring.mail.properties.mail.smtp.starttls.enable= true or false

As you can see in the above lines of syntax, we have to make these configurations inside the application file in order to read them and use them while sending the email to the user. In the coming section of the tutorial, we will have a closer look at each configuration in detail for better understanding for beginners.

How does Spring boot email work?

As we have already known, it enables the developers to send the email; this email can be sent in any of the requirements. We have some basic scenarios where every application needs this when we have any failure in the application of any exception or error that occurred in the application. In such cases, we can send emails to the developer in order to let them know about the error, with the specified logs and error message. Also, use as the notifier for the developer. In short, it can be used as the notifier service as well, apart from the usage for it depends on the requirement, because in the application we can have various requirements where we have to send email to user like, on successful registration, password reset and many more.

Let’s take a closer look at the different types of classes and interface available for this; see below;

1) MailSender Interface: It is a top-level interface that helps and provide us with the basic functionality by which we can send simple emails from our application

2) JavaMailSender Interface: This interface extends the MailSender, interface hence the sub-interface for it. It is basically used to prepare the MIMEMessage by the conjunction of the MimeMessageHelper class; It is very recommended to use one more interface, which is named MimeMessagePreparator.

3) JavaMailSenderImpl class: It is a class that helps us implement the JavaMailSender interface. Basically, it implements this interface; it supports SimpleMailMessage and MimeMessage.

4) SimpleMailMessage class: This is also a class that is used to create the simple message for mails such as text fields, cc, to, from ad subject.

5) MimeMessagePreparator interface: It is an interface that provides us with a callback interface by which we can prepare the MIME message.

6) MimeMessageHelper class: This class is a helper class that helps us to create the MIME message; it also provides us support for the image text content in HTML and mail attachments, etc.

Also, we add one dependency to enable ad use of the library for email; for reference, please see below;

e.g. :

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Examples of Spring boot email

Follow the below steps to configure email in the spring boot application, a simple application to send the emails by using the JavaSender, by giving the implementation to it.

a) Create a project by using spring initializer and provide all the details there, for reference find the URL below,

URL: https://start.spring.io/

b) Now, we have to add one more dependency in order to use the library for creating the email in spring boot; for reference, see below;

e.g.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

c) Now, in the application file, we have to put the detail in order to use this while sending the emails; for reference, see the below code; motioned you user and password for the SMTP account to make this work.

e.g. :

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your SMTP user
spring.mail.password=your SMTP password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

d) Paste the below code in the main class; this will give us the bean for JavaSender at the time of creation; just copy the code; nothing more is needed:

e.g. :

@SpringBootApplication
public class TradersApplication {
public static void main(String[] args) {
SpringApplication.run(TradersApplication.class, args);
}
@Bean
public Javasender getJavasender() {
JavasenderImpl sender = new JavasenderImpl();
sender.setHost("smtp.gmail.com");
sender.setPort(587);
sender.setUsername("your gamil acc");
sender.setPassword("your pass");
Properties pop = sender.getJavaMailProperties();
pop.put("mail.transport.protocol", "smtp");
pop.put("mail.smtp.auth", "true");
pop.put("mail.smtp.starttls.enable", "true");
pop.put("mail.debug", "true");
return sender;
}
}

e) create one simple service to send the email using Javasender bean we need to autowired the bean here in order to use it, for reference see below code;

e.g. :

@Component
public class EmailServiceDemoImpl implements EmailService {
@Autowired
private JavaMailSender sender;
public void sendEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("user1@educba.com");
message.setTo(to);
message.setSubject(subject);
message.setText(text);
sender.send(message);
System.out.pritln("Message sent successfully");
}
}

Output:

Spring boot email output

Conclusion

As we have seen how to enable the email in the spring boot application, adding one dependency and few lines of configuration is an easy way for developers to create the custom email in the application itself and send them using the Gmail transport layer security. We just have to specify the details of the SMTP users inside the application file in order to send. This is easy to learn, readable, and maintain as well.

Recommended Articles

This is a guide to Spring Boot Email. Here we discuss the working of Spring Boot Email along with an example and its implementation for better understating and clarity. You may also have a look at the following articles to learn more –

  1. Spring Boot HTTPS
  2. Spring Boot Profiles
  3. Spring Boot Dependencies
  4. Spring Boot Versions
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more