Introduction to JavaMail API
Email is everywhere in software — welcome messages, password resets, order confirmations, alerts, and reports. Java has had email support since 1997 through the JavaMail API (now Jakarta Mail), and modern frameworks like Spring Boot make it even easier.
In this chapter, you’ll learn what the JavaMail API is, the email protocols it supports, and when to use it vs alternatives.
What is the JavaMail API?
The JavaMail API is a platform-independent, protocol-independent framework for sending and receiving email from Java applications. Originally javax.mail, it’s now jakarta.mail under the Jakarta EE umbrella.
Email Protocols
| Protocol | Purpose | Port |
|---|---|---|
| SMTP | Sending email | 587 (TLS) / 465 (SSL) |
| IMAP | Reading email (server-side storage) | 993 (SSL) |
| POP3 | Reading email (download to client) | 995 (SSL) |
For most applications, you only need SMTP (sending). Use IMAP if you need to read incoming emails programmatically.
JavaMail vs Spring Boot Email vs Third-Party APIs
| Approach | When to Use |
|---|---|
| JavaMail API | Non-Spring projects, full control |
| Spring Boot Mail | Spring Boot projects (wraps JavaMail with simpler API) |
| SendGrid / Mailgun / AWS SES | High-volume transactional email, deliverability tracking |
What’s Next?
In the next chapter, you’ll set up JavaMail with Maven and configure SMTP for Gmail and Mailtrap.