Setup and Using Email Service in Spring Boot
Weather it's for account verification, password recovery, notifications and many more, sending email is a cruicial feature. we'll walk through how to setup and use an email service in a Spring Boot project using JavaMailSender.
Follow the Steps:
Step 1: Adding Dependencies
To enable email functionality in Spring Boot, we need to add the Spring Boot Starter Mail dependency to our pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
If you are using Gradle, add the following to build.gradle:
implementation 'org.springframework.boot:spring-boot-starter-mail'
Step 2: Set your App Password
For Security reasons, It is recommanded to use App Password instead of your actual password when using gmail.
goto your Google Account Manager and follow the image process
Step 3: Configuring Mail Properties
Spring Boot makes email configuration simple by using the application.properties
or application.yml file.
For example, to use Gmail SMTP, add the following to src/main/resources/application.properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username= [email protected]
spring.mail.password= your-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Step 4: Creating the Email Service
Now, let’s create a MailService class to send emails:
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 MailService {
@Autowired
private JavaMailSender mailSender;
public void sendEmail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
message.setFrom("[email protected]");
mailSender.send(message);
}
}
Step 5: Sending an Email from a Controller
To test the email service, let’s create a MailController:
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 MailController { @Autowired private MailService mailService; @GetMapping("/send-email") public String sendEmail(@RequestParam String to, @RequestParam String subject, @RequestParam String body) { mailService.sendEmail(to, subject, body); return "Email sent successfully!"; } }
Step 6: Testing the Email Service
Run your Spring Boot application and open a browser or use Postman to test the API:
http://localhost:8080/[email protected]&subject=Hello&body=This is a test email
Step 7 : Using HTML Templates (Optional)
For better-looking emails, you can use Thymeleaf or Freemarker templates. Here’s an example of sending an HTML email:
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Service
public class HtmlMailService {
@Autowired
private JavaMailSender mailSender;
public void sendHtmlEmail(String to, String subject, String htmlBody) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(htmlBody, true);
mailSender.send(message);
}
}
Conclusion
Spring Boot makes it easy to integrate an email service into your application. By following these steps, you can send simple text emails as well as HTML-based emails with templates. This is useful for various functionalities like user verification, password resets, and notifications.
🔹 Now you can send emails seamlessly in your Spring Boot projects! 🚀
Comments (0)