In this post, We will learn How to Send Mail Spring Boot Example using a demo Project.
Spring Boot Starter email is a generally thin wrapper over Jakarta Mail API that makes very easy integration with emailing services into Spring-based applications. so here we will be using Spring starter email dependency in pom.xml to work with email functionality.
We will be adding below Spring boot starter email dependency to work with sending a mail from the spring boot application in maven based project.
1 2 3 4 5 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> |
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?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.5.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kkhindigyan.app</groupId> <artifactId>SpringBootSendingEmailDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBootSendingEmailDemo</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
In Order to send mail, we have to Configure SMTP Properties in the application’s properties file as below.
We have configured Gmail’s SMTP server details with SMTP TLS enable as true. Here as per your requirement, If you can also configure the settings of your own SMTP server. In case you are using Gmail then you must enable SMTP in your Gmail profile/account settings to generate an access password (not your email password) for better security reason.
application.properties
1 2 3 4 5 6 |
spring.mail.host=smtp.gmail.com spring.mail.port=587 spring.mail.username=kishan.javatrainer@gmail.com spring.mail.password=set your password spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true |
EmailSenderService.java
Spring Mail API has a JavaMailSender interface that gives important methods for sending emails. After configuring SMTP server details in our Application’s properties file, We can ask Spring framework to inject the default implementation of JavaMailSender into an EmailSenderService class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.kkhindigyan.app.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.stereotype.Service; @Service public class EmailSenderService { @Autowired private JavaMailSender mailSender; public void sendEmail(String fromEmail,String toEMail,String subject,String body) { SimpleMailMessage mailMessage = new SimpleMailMessage(); mailMessage.setFrom(fromEmail); mailMessage.setTo(toEMail); mailMessage.setSubject(subject); mailMessage.setText(body); mailSender.send(mailMessage); System.out.println("Email Sent Successfully!!"); } } |
SpringBootSendingEmailDemoApplication.java
In the below Spring BootStrap class, we are asking Spring Container to return EmailSenderService bean so that we can invoke sendEmail(-,-,-,-) method to test email functionality.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.kkhindigyan.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.kkhindigyan.app.service.EmailSenderService; @SpringBootApplication public class SpringBootSendingEmailDemoApplication { public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringBootSendingEmailDemoApplication.class, args); EmailSenderService emailSenderService = applicationContext.getBean(EmailSenderService.class); emailSenderService.sendEmail("[email protected]", "[email protected]", "Sending email using Spring Booot", "Hey, How are you!! Sending an email for testing purpose!!"); } } |
That’s all about How to Send Mail Spring Boot Example
You May Also Like:
What is Spring boot ?
Spring vs Spring boot
Spring Boot Actuator
How to Configure Multiple Data Sources in a Spring Boot?
Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
If you have any feedback or suggestion please feel free to drop in below comment box.