Introduction
Imagine Forgetting your Password and being unable to log into your favorite social media application, What comes to the rescue is a Forgot Password Button, By entering your email address it sends you an email with a reset password link.
So behind the scenes what happened here is application code sent an email to your email address with your account matching info to verify your identity. So nowadays we have a lot of tools for sending and receiving messages but still, email remains a vital communication tool for a lot of applications, From account opening in an application to order placement every time an email comes into the picture.
From Social media Platforms to banking applications and e-commerce applications, all use emails for some of their work.
Fortunately, Java developers have a powerful ally in Spring Boot, a framework that simplifies email sending through easily implementable libraries and configurations.
In this article, we’ll dive into the world of email sending with Java and Spring Boot, exploring how to seamlessly integrate this essential functionality into your applications
How Mail Transfer works
First of all, after we compose our mail and click on the Send Button Below steps start one by one:
In Your Computer:
- Authentication: We use some mail clients (Gmail, Outlook, etc.) that connect with the mail server using SMTP protocol and authorize using username /password. This is a short-lived connection just for authorization purposes like checking the identity of the user and their permissions.
- Mail Client converts your message (text attachments) into MIME format.
- Mail client establishes a connection with mail server using TLS/SSL encryption, for securely transmitting the message content. This is a long-lived connection
- Mail Server then validates the receipt email address and then using DNS it finds the receipt mail server and puts the message into the queue for delivery.
In Internet:
- Your mail server connects with the receipts mail server using SMTP protocol.
- The receipt Mail server verifies the receipt email address and stores the mail in their mailbox.
On Receipt Computer:
- The mail client sends a notification as per settings about the email.
- Once the user clicks to open an email, It uses POP3 or IMAP protocol to fetch messages from the server.
These above steps are internal workings of mail delivery.
Send Email Using Java – Spring Boot
To enable email functionality within Java applications, developers can utilize the JavaMail API, a dedicated library provided by the Java platform. JavaMail API provides different methods to send email with content.
Refer:
JavaMail API
Spring Boot is the widely used Java framework for web application development. Spring provides starter libraries for different operations like web, data-jpa, test, etc. Similarly, it has a library for sending email which is known as spring-boot-starter-mail.
This library internally uses JavaMail API for email delivery. It needs a message and a basic configuration for sending the email.
Steps to Send Email
- Add dependency in your Maven or Gradle project for spring-boot-starter-mail.
- Provide SMTP server details in the application properties file.
- Autowire the JavaMailSender bean, this uses the default properties mentioned in the properties file for sending email.
- Now Create a method to construct SimpleMailMessage Object and set basic properties like subject, receipt address, cc, bcc, and content.
- Now this SimpleMailMessage object will passed as an argument in the JavaMailSender bean’s send method to send the email
Code Sample
- Dependency in build.gradle file
implementation 'org.springframework.boot:spring-boot-starter-mail:1.2.0.RELEASE'
2. Add basic properties in yml file
spring:
mail:
host: smtp.gmail.com
port: 587
username: cshubtiwari@gmail.com
password: abcdedf
properties:
mail:
smtp:
auth: true
starttls.enable: true
ssl:
protocols: TLSv1.2
3. Autowire JavaMailSender and create a message to send an email
@Autowired
private final JavaMailSender javaMailSender;
@Override
public void sendEmail(String email) {
String resetLink = "This is test email";
SimpleMailMessage mailMessage = new SimpleMailMessage();;
mailMessage.setFrom("cshubtiwari@gmail.com");
mailMessage.setTo(email); // Recipient's email address
mailMessage.setSubject("Password Reset Link");
mailMessage.setText("Click the link to reset your password: " + resetLink);
// Send the email
javaMailSender.send(mailMessage);
}
So after following the above steps just execute the code the email will be delivered.