J2EE applications often need to send email notifications (order confirmations, password resets, alerts). Without a standard API, developers must deal with vendor-specific email protocols (SMTP, POP3, IMAP) directly.
JavaMail enables sending email messages in a platform-independent, protocol-independent manner from Java programs. It depends on JavaBeans Activation Framework (JAF), which also becomes part of J2EE.
- Protocol-independent: Works with SMTP, POP3, IMAP without code changes
- Session creation: Obtain
javax.mail.Session(often via JNDI) - Message construction: Create
MimeMessagewith subject, body, recipients - Transport: Use
Transport.send()to send the message - J2EE integration: Use in servlets, JSP, or EJB components
Example: Send order confirmation email from an e-commerce site.
- Platform-independent: Works on any Java platform
- Protocol-independent: Supports SMTP, POP3, IMAP
- JAF dependency: Requires JavaBeans Activation Framework
- J2EE standard: Part of J2EE platform
- Server-side use: Commonly used in J2EE deployments
- Built from: Java Platforms — JavaMail is part of J2EE
- Related: EJB Container — EJB components can use JavaMail
- Related: Servlets — servlets commonly send emails
- Related: JSP — JSP pages can trigger email sending
- JAF required: Forgetting JAF causes ClassNotFound errors
- SMTP configuration: Must configure SMTP host correctly
- Authentication: SMTP may require username/password
- Not covered: The source book does not cover JavaMail in detail