~~ Spring Boot ~~
1. Introduction
1.1 Spring Boot-Intorduction
1.2 Spring Boot-Architecture
1.2 Spring Boot-Quick Start
2. Project Creation
2.1 Spring Initializer
2.2 Spring Boot CLI
2.3 STS IDE
2.4 Eclipse IDE
2.5 First Program
2.6 Spring Boot-Code Structure
3. Spring Boot – Beans & DI
4. Spring Boot – Runners
5. Spring Boot – Properties File
6. Spring Boot – Looging
7. Spring Boot – Rest WebServices
8. Spring Boot – Exception Handling
9. Spring Boot – Interceptor
10. Spring Boot – Servlet Filter
11. Spring Boot – Rest Template
12. Spring Boot – File HANDLING
13. Spring Boot – Thymeleaf
14. Spring Boot – Internationalization
15. Spring Boot – Scheduling
16. Spring Boot – Actuator
17. Spring Boot – Email Handling
18. Spring Boot – Database Handling
19. Spring Boot – Caching
Spring Boot | Email Handling :
In Spring Boot we can handle Email related task also. For this purpose we have “JavaMailSender” Interface. Using this Interface we can implement email handling.
Here we will learn how to send emails from Spring Boot to Gmail, plain text and as well as attachment also.
Sending Simple Text to Gmail :
Below is our Project Structure, where we are going to implement.

First we need to add below dependency to our pom.xml file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency>
After adding these dependency, we will configure our application.properties file. To configure SMTP details, as below.
#Email configuration
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=****@gmail.com
spring.mail.password=**********
# Other properties
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true
Note :
Here under spring.mail.username, you have to mention your gmail id and spring.mail.password mention your gmail password.
package com.study4online;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EmailApplication {
public static void main(String[] args) {
SpringApplication.run(EmailApplication.class, args);
}
}
package com.study4online.model;
import javax.validation.constraints.Email;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class Feedback {
@NotNull
private String name;
@NotNull
private String email;
@NotNull
@Min(10)
private String message;
public Feedback() {
}
public Feedback(String name, String email, String message) {
super();
this.name = name;
this.email = email;
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Feedback [name=" + name + ", email=" + email + ", message=" + message + "]";
}
}
package com.study4online.service;
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.Service;
import com.study4online.config.EmailConfig;
import com.study4online.model.Feedback;
@Service
public class EmailService {
Logger logger = LoggerFactory.getLogger(EmailService.class);
@Autowired
private JavaMailSender javaMailSender;
private EmailConfig emailConfig;
public EmailService(EmailConfig emailConfig) {
this.emailConfig = emailConfig;
}
public void sendEmail(Feedback feedback) throws Exception {
// Create an email instance
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom(feedback.getEmail());
mailMessage.setTo("[email protected]");
mailMessage.setSubject("Feedback From " + feedback.getName());
mailMessage.setText("Name : " + feedback.getName() + "\n" + "Email : " + feedback.getEmail() + "\n"
+ "Message : " + feedback.getMessage());
// Send mail
javaMailSender.send(mailMessage);
logger.info("Email sent successfully");
}
}
package com.study4online.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class EmailConfig {
EmailConfig() {
System.out.println("EmailConfig default constructor");
}
@Value("${spring.mail.host}")
private String host;
@Value("${spring.mail.port}")
private int port;
@Value("${spring.mail.username}")
private String username;
@Value("${spring.mail.password}")
private String password;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "EmailConfig [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
+ "]";
}
}
Now we are done with our code, below is request which we will try to hit from Postman.
{
"name": "Varun Kumer",
"email": "[email protected]",
"message": "Hi This is dummy mail for testing purpose."
}

After running this program, we can check in our gmail account, wow we received that mail as below.

NOTE :
If we are using correct gmail username and password in code but still geeting some error related to Authentication failed, then we need to make Allow Less Secure Apps as ON. Use below link to make it on.
https://myaccount.google.com/lesssecureapps
After test your application, make sure turn it Off, because it’s not recommended to make it On because of Security reason.