LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1918 min read

Email Dependencies

Learn how to send transactional emails from a Spring Boot application using spring-boot-starter-mail, including plain text and HTML emails.

Introduction

Almost every real application needs to send email at some point — a welcome email after signup, a password reset link, an order confirmation, or an alert when something goes wrong. Spring Boot ships a dedicated starter for exactly this: spring-boot-starter-mail.

It wraps the JavaMail API in a much friendlier Spring abstraction called JavaMailSender, and it auto-configures itself from a handful of properties, so you can go from "no email support" to "sending emails" in a few minutes.

What You Will Learn
  • What problem spring-boot-starter-mail solves.
  • How to add it to your project and configure an SMTP server.
  • How to send a simple plain text email with JavaMailSender.
  • How to send a richer HTML email using MimeMessage and MimeMessageHelper.

Why You Need This Dependency

Use case: any feature that needs to notify a user outside the application itself — account verification, password resets, invoices, marketing digests, or system alerts to an operations team. Without this starter, you would have to manually configure the raw JavaMail API, which is verbose and easy to get wrong (session properties, transport protocols, authenticators).

Adding the Dependency

Add spring-boot-starter-mail to your pom.xml. It pulls in the Jakarta Mail API and Spring's mail abstraction, and Spring Boot auto-configures a JavaMailSender bean as soon as mail properties are present.

pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

Configuring Mail Properties

Configure your SMTP server in application.properties. Here is a typical Gmail SMTP setup (for production, use a transactional email provider such as SendGrid, Amazon SES, or Mailgun instead of a personal Gmail account).

application.properties
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=true
Never Commit Real Credentials

Keep spring.mail.username and spring.mail.password out of source control. Load them from environment variables or a secrets manager, and use an app-specific password rather than your real account password.

Sending a Simple Email

Once the properties above are present, Spring Boot auto-configures a JavaMailSender bean you can inject anywhere. SimpleMailMessage is the easiest way to send a plain text email.

EmailService.java
@Service
public class EmailService {
private final JavaMailSender mailSender;
public EmailService(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendWelcomeEmail(String to, String username) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject("Welcome to PrograMinds!");
message.setText("Hi " + username + ",\n\nThanks for signing up. Happy learning!");
mailSender.send(message);
}
}
Result

Click Run to see what this code prints.

Sending an HTML Email

Most real-world emails need formatting, links, and images, which SimpleMailMessage cannot do. For that, build a MimeMessage and use MimeMessageHelper to set HTML content.

EmailService.java
public void sendOrderConfirmation(String to, String orderId) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
String html = """
<h2>Order Confirmed</h2>
<p>Your order <strong>%s</strong> has been placed successfully.</p>
<p><a href=\"https://example.com/orders/%s\">Track your order</a></p>
""".formatted(orderId, orderId);
helper.setTo(to);
helper.setSubject("Order Confirmation - " + orderId);
helper.setText(html, true);
mailSender.send(message);
}
Keep Sending Async

Sending mail is a network call to an external SMTP server and can be slow. Annotate the calling method (or this method) with @Async so email sending does not block the request thread — pair it with @EnableAsync on a configuration class.

Common Mistakes

Avoid These Mistakes
  • Hardcoding SMTP credentials directly in application.properties instead of using environment variables.
  • Forgetting helper.setText(html, true) — the boolean flag is what tells Spring the content is HTML, not plain text.
  • Sending emails synchronously inside a request thread, slowing down the user-facing response.
  • Using a personal email account for production traffic instead of a dedicated transactional email provider.

Best Practices

  • Externalize SMTP host, port, username, and password as environment-specific configuration.
  • Use a templating engine like Thymeleaf to build HTML email bodies instead of concatenating strings.
  • Wrap email sending in try/catch and log failures — a failed email should rarely fail the entire user-facing operation.
  • Use a dedicated transactional email provider (SendGrid, SES, Mailgun) in production for deliverability and rate limits.

Frequently Asked Questions

No. Tools like Mailhog or a local SMTP debug server let you capture emails without actually delivering them, which is ideal for local development and testing.

Yes. MimeMessageHelper has an addAttachment(filename, file) method for attaching files to a MimeMessage.

No, spring-boot-starter-mail only handles sending. Pair it with spring-boot-starter-thymeleaf to render HTML templates into strings before passing them to MimeMessageHelper.

Summary

spring-boot-starter-mail gives you a JavaMailSender bean auto-configured from simple properties, letting you send both plain text and rich HTML emails with just a few lines of code.

Lesson 19 Completed
  • You understand when to reach for spring-boot-starter-mail.
  • You configured SMTP properties and sent a plain text email.
  • You built an HTML email using MimeMessage and MimeMessageHelper.
Next Lesson →

Messaging Dependencies (RabbitMQ & Kafka)