• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

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.

Core Idea

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.

How It Works

  1. Protocol-independent: Works with SMTP, POP3, IMAP without code changes
  2. Session creation: Obtain javax.mail.Session (often via JNDI)
  3. Message construction: Create MimeMessage with subject, body, recipients
  4. Transport: Use Transport.send() to send the message
  5. J2EE integration: Use in servlets, JSP, or EJB components

Example: Send order confirmation email from an e-commerce site.

Visual Explanation

G J2EE App J2EE App JavaMail API JavaMail API J2EE App->JavaMail API send email JAF JAF JavaMail API->JAF depends on SMTP Server SMTP Server JavaMail API->SMTP Server SMTP User's Inbox User's Inbox SMTP Server->User's Inbox

Key Properties

  • 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

Connections

  • 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

Edge Cases & Gotchas

  • 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