Send an Email With Spring Boot

Durjoy Acharjya
3 min readMar 12, 2024

--

Hello Readers

This is another blog from Durjoy Acharya today I’m going to show you how to send email using SpringBoot first we need to the spring starter and just go to the spring initializer.

Add Spring web & Java Mail Sender and click generate
<?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>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.app</groupId>
<artifactId>mailSender</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mailSender</name>
<description>mailSender</description>
<properties>
<java.version>21</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-web</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>

Let’s move on coding section…..

First, we need to create a service class and annotate it with @Service

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
public final JavaMailSender javaMailSender;
public EmailService(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public void sendEmail(String toEmail,String subject,String body){
var msg=new SimpleMailMessage();
msg.setTo(toEmail);
msg.setSubject(subject);
msg.setText(body);
javaMailSender.send(msg);
System.out.println("Email Send Successfully");
}
}

Now we need to configure application.properties

spring.mail.host=smtp.gmail.com
string.mail.port=587
spring.mail.username=durjoy.cse.cu@gmail.com # your mail id(gmail)
spring.mail.password=<get from gmail> # follow given step
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

just go to your Gmail account => Manage your google account => Security => 2-Step Verification then turn on give your app name and get the password

follow the steps

Get the password remove the space and paste the password to spring.mail.password

Inject the service class as you need


import com.app.mailsender.communication.email.EmailService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

@SpringBootApplication
public class MailSenderApplication {
public final EmailService emailService;
private static String body="Hi this is Durjoy Acharya a lazy programmer and open-source contributor";
public MailSenderApplication(EmailService emailService) {
this.emailService = emailService;
}
public static void main(String[] args) {
SpringApplication.run(MailSenderApplication.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void sendMail(){
emailService.sendEmail("da-durjoy@outlook.com",null,body);
}
}

Finally, Run The Application and Boom

Check the Magic now check the mail

Got Mail

Thank you if the post is helpfull please follow Durjoy Acharya

Social Links Github Linkedin

--

--

Durjoy Acharjya
Durjoy Acharjya

Written by Durjoy Acharjya

🚀 Passionate Programmer | 🌐 Angular | ⚡ .NET Core | ☁️ Spring Boot | 🔧 AWS | 🔒 DevOps | 🌱 MLOps | 🛡️ DevSecOps | 📊 T-SQL | 📡 Kafka 📱 Swift | 🍏 iOS

No responses yet