Sending an email using Spring Boot with JavaMailSender is a common task for many applications. Below is a step-by-step guide to help you achieve this.
1. Spring Boot and JavaMailSender
Spring Boot is a framework that allows for the rapid creation of standalone, production-grade Spring-based applications. JavaMailSender is an interface provided by Spring Framework which simplifies the sending of emails using the JavaMail API.
2. Set up your Spring Boot project
If you haven't already, create a Spring Boot project using Spring Initializr or your preferred method. Make sure to include the following dependencies.
- Spring Web
- Spring Boot Starter Mail
To use JavaMailSender, you need the Spring Boot Starter Mail dependency. This includes all necessary libraries to send emails via SMTP.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<!-- Spring Boot Starter Mail -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
3. Configure Mail Properties
Configure your email properties in application.properties or application.yml. For example, if you're using Gmail's SMTP server.
spring.mail.host=smtp.gmail.com
spring.mail.port=587
[email protected]
spring.mail.password=your-email-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
4. Create an Email Service
An email service class is used to define methods that utilize JavaMailSender to send emails. SimpleMailMessage is a class provided by Spring to create simple text-based emails.
Create a service class to handle sending emails. For example.
package com.example.demo.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 EmailService {
@Autowired
private JavaMailSender javaMailSender;
public void sendSimpleEmail(String to, String subject, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(text);
javaMailSender.send(message);
}
}
5. Create a Controller
A controller class is used to expose endpoints that can be called to send emails. This is where you can trigger the email-sending process.
Create a controller to trigger the email sending.
package com.example.demo.controller;
import com.example.demo.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmailController {
@Autowired
private EmailService emailService;
@GetMapping("/sendEmail")
public String sendEmail(
@RequestParam String to,
@RequestParam String subject,
@RequestParam String text) {
emailService.sendSimpleEmail(to, subject, text);
return "Email sent successfully";
}
}
6. Run your Application and Test the Email Sending
Run your Spring Boot application. You can do this from your IDE or by using the command line with mvn spring-boot: run.
Or
Run your Spring Boot application and navigate to http://localhost:8080/[email protected]&subject=Test&text=Hello.
You should see a message indicating that the email was sent successfully.
Full Project Structure
src
├── main
│ ├── java
│ │ └── com
│ │ └── example
│ │ └── demo
│ │ ├── DemoApplication.java
│ │ ├── controller
│ │ │ └── EmailController.java
│ │ └── service
│ │ └── EmailService.java
│ └── resources
│ ├── application.properties
│ └── static
└── test
└── java
└── com
└── example
└── demo
└── DemoApplicationTests.java
Troubleshooting
- Authentication Issues: If you are using Gmail, ensure that you allow "Less secure app access" or set up an App password for your Google account.
- Port Issues: Ensure that port 587 (or 465 for SSL) is open on your network.
- Dependencies: Ensure all necessary dependencies are correctly added to your project.
Important Notes
- Security: When using Gmail, you may need to allow "Less secure app access" or generate an App password to allow your Spring Boot application to send emails.
- Dependencies: Ensure all necessary dependencies are correctly added to your project.
- Port and Network Issues: Ensure that the port used for SMTP (587 for TLS or 465 for SSL) is open and accessible on your network.
Conclusion
With these steps, you should be able to set up and send emails using Spring Boot and JavaMailSender.
This guide should help you set up a basic email-sending functionality using Spring Boot and JavaMailSender. If you need to send more complex emails (e.g., with attachments or HTML content), you'll need to use the MimeMessage class provided by JavaMailSender.