This is a latest guide on how to send Email in your SpringBoot Application. Simply follow these steps to make your Spring Application be able to send Email.
Step 1: Add Project Dependency
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Step 2: Set up App Password inside your Google Account
- Go to (Google Account)[https://myaccount.google.com]
- On Search Bar, search for App Passwords then you should see App Passwords with Security under it
- Under Select App, find Other (Custom Name), input your desired name then click Generate
- Copy the password we will use it later
Step 3: Set up Application.properties
spring.mail.username=youremail@gmail.com
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.password=Copied Password From App Password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.starttls.enable=true
Step 4: Configure Bean for JavaMailSender
@Configuration
public class MailConfiguration {
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("your@gmail.com");
mailSender.setPassword("copiedPassword");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
}
Step 5: Implement MailSenderService
@Service
public class MailSenderService {
@Autowired
private JavaMailSender mailSender;
public void sendNewMail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
}
Step 6: Start sending email
private final MailSenderService mailService;
public void Foo(){
mailService.sendNewMail("test@gmail.com", "Subject right here", "Body right there!")
}
I hope this guide will help you. Thanks for reading!
Top comments (7)
i think doing step 4 is enough without doing the Step 3,isn't ?
because the Java configuration class will override the settings from the application properties or YAML file
and thanks for the guide
You are correct, thanks for the correction
All guides must be like this.
Thanks for sharing @vatana7 ,
I would add spring.mail.properties.smtp.ssl.enable=true
Chances are that SMTP connection will fail without this when sending emails.
Thanks!
the code is exactly the same, but I still get the error Failed message 1: org.eclipse.angus.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
Did you follow step 2 correctly? It should be able to send if follow these steps