How to Implement E-mail sending service with Spring-Boot
4 min readMay 9, 2021
using Mailtrap web service.
There are few ways to implement E-mail sending service in Spring-Boot. Today we are going to discuss how to implement it by using Mailtrap online E-mail sending function.
- Through this blog I use the MVC architecture to implementations.
- EntelliJ IDEA ultimate 2020.3.2 use for Spring-boot implementations.
- Required web site links and references added at the end of the blog.
1. First of all you need to create an free account in Mailtrap E-mail service
- By going through that sign-up button, you can create a new mail trap account.
- After creating the account you can see the configuration details inside the your account.
2. Add dependencies and Configure Mailtrap account details and credentials in Spring-Boot project
- You have to add below dependencies to
pom.xml
file in the project.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- After that you have to configure mailtrap account inside the
application.properties
file in the project.
- Then create a
EmailConfiguration
class inside the model folder and declare the attributes using to configure mailtrap account.
- Set the values of the attributes by getting from the
application.properties
file which configure the mailtrap account details. - Generate the getters and setters from the declared variables.
3. Implementation of the E-mail service
- Create an
EmailFeedback
class inside the model class. That is the model of E-mail feedback object which is sending from the frontend of the application. - Generate the setters and getters from the declared attributes also.
- Then create an
FeedbackController
class to implement the service.
- Before implement this class you have to import below libraries. Otherwise it will show some errors in the code. As well as you have to import
EmailConfiguration
andEmailFeedback
classes which created before inside the model file.
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;import org.springframework.validation.BindingResult;import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
If you use the IntelliJ Idea for this implementations, no need to add those imports to class because it automatically add them to it. otherwise you have to add them manually.
- Now E-mail Service implementation part is almost completed. Now you need to test the service working properly.
4. Testing the Service work properly
- To do this testing part, you can use third-party software like postman or create a testing file inside the project.
- In this time I use the testing file to test the service work properly.
- To do that, create a file call
EmailFeedback.http
inside the project and create a API request to the email service part like below snapshot.
- Then run the Spring-Boot project. And run this testing file next.
- If it is working properly you will get the response like given below.
- Then check the Mailtrap account and you will be reserved an E-mail according your request.
Required Website links:
- Mailtrap signup : https://mailtrap.io
- If you want to know what is the MVC architecture is, please check this one : https://minindukothalawala.medium.com/mvc-architecture-7145ee90ef0f
Thank you for reading this blog and I hope you get an idea about the E-mail service implementation in Spring-Boot. Please feel free to leave a comment if you have any doubts according to this implementation, I will try to help you as I can. Stay safe.