Sending Emails with Spring
Password resets, welcome emails, invoices. Send them all with Spring.
The Hyderabad Post Office Analogy:
If you want to send a letter, you don’t walk to the recipient’s house yourself. You write the letter, put it in an envelope, write the address, and drop it at the local Post Office. The post office handles the delivery.
In Spring Boot, JavaMailSender is the local Post Office. You give it the recipient’s email, the subject, and the message. It connects to an SMTP server (like Gmail or Amazon SES) which actually delivers the email across the internet.
- spring-boot-starter-mail: The dependency that auto-configures
JavaMailSender. - SimpleMailMessage: Used for plain text emails.
- MimeMessage: Used for complex emails with attachments or HTML content.
- SMTP Properties: Configured in
application.propertiesto tell Spring which mail server to use.
First, add the mail dependency to pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency># application.properties (Gmail SMTP Setup)
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your-email@gmail.com
spring.mail.password=your-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=truepackage com.devinhyderabad;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
@Service
public class EmailService {
private final JavaMailSender mailSender;
public EmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
// 1. Simple Plain Text Email
public void sendSimpleEmail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
// 2. HTML Email (with styling)
public void sendHtmlEmail(String to, String subject) throws MessagingException {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setTo(to);
helper.setSubject(subject);
String htmlContent = "<h1 style=’color:blue;’>Welcome to DevInHyderabad!</h1>" +
"<p>Thank you for registering. Click <a href=’https://devinhyderabad.com’>here</a> to verify.</p>";
helper.setText(htmlContent, true); // ’true’ enables HTML
mailSender.send(mimeMessage);
}
}Interview Question: “How do you handle sending thousands of emails without slowing down the application?”
Answer: Sending emails is a slow network I/O operation. If you send emails synchronously in a REST controller, the user will wait seconds for the response. You should combine @Async and JavaMailSender. The API returns immediately, and a background thread sends the emails.
Enterprise Note: For corporate apps, never use your personal Gmail account. Use enterprise SMTP providers like Amazon SES, SendGrid, or Mailgun. They provide APIs, bounce handling, and analytics. Also, remember to set the from address properly to avoid being marked as spam.
Key Takeaways
- ✅ spring-boot-starter-mail auto-configures JavaMailSender
- ✅ SimpleMailMessage for plain text, MimeMessage for HTML/attachments
- ✅ Configure SMTP host, port, and credentials in application.properties
- ✅ Combine @Async with JavaMailSender for sending emails in the background
- ✅ Use enterprise SMTP providers (SES, SendGrid) for production apps
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login