Jakarta Mail defines a platform-independent and protocol-independent framework to build mail and messaging applications.
—
Core mail operations provide essential session management, message handling, and transport functionality for Jakarta Mail applications.
The Session class is the entry point for Jakarta Mail operations, managing configuration properties and protocol providers.
public final class Session {
// Create new session instances
public static Session getInstance(Properties props);
public static Session getInstance(Properties props, Authenticator authenticator);
// Get default shared session (thread-safe)
public static Session getDefaultInstance(Properties props);
public static Session getDefaultInstance(Properties props, Authenticator authenticator);
// Protocol provider access
public Store getStore() throws NoSuchProviderException;
public Store getStore(String protocol) throws NoSuchProviderException;
public Store getStore(URLName url) throws NoSuchProviderException;
public Store getStore(Provider provider) throws NoSuchProviderException;
public Transport getTransport() throws NoSuchProviderException;
public Transport getTransport(String protocol) throws NoSuchProviderException;
public Transport getTransport(URLName url) throws NoSuchProviderException;
public Transport getTransport(Address address) throws NoSuchProviderException;
public Transport getTransport(Provider provider) throws NoSuchProviderException;
public Folder getFolder(URLName url) throws MessagingException;
// Provider management
public Provider[] getProviders();
public Provider getProvider(String protocol) throws NoSuchProviderException;
public void setProvider(Provider provider) throws NoSuchProviderException;
public void addProvider(Provider provider);
// Authentication management
public void setPasswordAuthentication(URLName url, PasswordAuthentication pw);
public PasswordAuthentication getPasswordAuthentication(URLName url);
public PasswordAuthentication requestPasswordAuthentication(InetAddress addr, int port, String protocol, String prompt, String defaultUserName);
// Configuration access
public Properties getProperties();
public String getProperty(String name);
// Debug support
public void setDebug(boolean debug);
public boolean getDebug();
public PrintStream getDebugOut();
public void setDebugOut(PrintStream out);
}import jakarta.mail.*;
import java.util.Properties;
// Basic session creation
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username@gmail.com", "password");
}
});
// Enable debug output
session.setDebug(true);The Message abstract class provides the core interface for email message manipulation.
public abstract class Message implements Part {
// Message identification
public Session getSession();
public int getMessageNumber();
public Folder getFolder();
public boolean isExpunged();
// Address management
public abstract Address[] getFrom() throws MessagingException;
public abstract void setFrom() throws MessagingException;
public abstract void setFrom(Address address) throws MessagingException;
public abstract void addFrom(Address[] addresses) throws MessagingException;
public abstract Address[] getRecipients(RecipientType type) throws MessagingException;
public Address[] getAllRecipients() throws MessagingException;
public abstract void setRecipients(RecipientType type, Address[] addresses) throws MessagingException;
public void setRecipient(RecipientType type, Address address) throws MessagingException;
public abstract void addRecipients(RecipientType type, Address[] addresses) throws MessagingException;
public abstract void addRecipients(RecipientType type, String addresses) throws MessagingException;
public void addRecipient(RecipientType type, Address address) throws MessagingException;
public Address[] getReplyTo() throws MessagingException;
public void setReplyTo(Address[] addresses) throws MessagingException;
// Subject and dates
public abstract String getSubject() throws MessagingException;
public abstract void setSubject(String subject) throws MessagingException;
public abstract Date getSentDate() throws MessagingException;
public abstract void setSentDate(Date date) throws MessagingException;
public abstract Date getReceivedDate() throws MessagingException;
// Message flags
public abstract Flags getFlags() throws MessagingException;
public boolean isSet(Flags.Flag flag) throws MessagingException;
public abstract void setFlags(Flags flag, boolean set) throws MessagingException;
public void setFlag(Flags.Flag flag, boolean set) throws MessagingException;
// Message operations
public abstract Message reply(boolean replyToAll) throws MessagingException;
public abstract void saveChanges() throws MessagingException;
public boolean match(SearchTerm term) throws MessagingException;
// Content operations (inherited from Part)
public Object getContent() throws IOException, MessagingException;
public String getContentType() throws MessagingException;
public int getSize() throws MessagingException;
public InputStream getInputStream() throws IOException, MessagingException;
}public static final class Message.RecipientType implements Serializable {
public static final RecipientType TO;
public static final RecipientType CC;
public static final RecipientType BCC;
public String toString();
}import jakarta.mail.*;
import jakarta.mail.internet.*;
// Create message
MimeMessage message = new MimeMessage(session);
// Set addresses
message.setFrom(new InternetAddress("sender@example.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipient1@example.com,recipient2@example.com"));
message.setRecipients(Message.RecipientType.CC,
InternetAddress.parse("cc@example.com"));
// Set subject and content
message.setSubject("Test Message");
message.setText("This is a test message body");
// Set sent date
message.setSentDate(new Date());
// Save changes before sending
message.saveChanges();The Transport abstract class handles message transmission to recipients.
public abstract class Transport extends Service {
// Constructor
public Transport(Session session, URLName urlname);
// Static convenience methods
public static void send(Message msg) throws MessagingException;
public static void send(Message msg, Address[] addresses) throws MessagingException;
public static void send(Message msg, String user, String password) throws MessagingException;
public static void send(Message msg, Address[] addresses, String user, String password) throws MessagingException;
// Instance methods
public abstract void sendMessage(Message msg, Address[] addresses) throws MessagingException;
// Event handling
public void addTransportListener(TransportListener l);
public void removeTransportListener(TransportListener l);
// Service connection methods (inherited)
public void connect() throws MessagingException;
public void connect(String host, String user, String password) throws MessagingException;
public void connect(String user, String password) throws MessagingException;
public boolean isConnected();
public void close() throws MessagingException;
}import jakarta.mail.*;
import jakarta.mail.internet.*;
// Simple send using static method
Transport.send(message);
// Send to specific addresses only
Address[] specificRecipients = {
new InternetAddress("recipient@example.com")
};
Transport.send(message, specificRecipients);
// Using transport instance for multiple sends
Transport transport = session.getTransport("smtp");
try {
transport.connect("smtp.gmail.com", username, password);
transport.sendMessage(message1, message1.getAllRecipients());
transport.sendMessage(message2, message2.getAllRecipients());
} finally {
transport.close();
}The Service abstract class provides common functionality for Store and Transport.
public abstract class Service {
// Connection management
public void connect() throws MessagingException;
public void connect(String host, String user, String password) throws MessagingException;
public void connect(String user, String password) throws MessagingException;
public boolean isConnected();
public void close() throws MessagingException;
// Service properties
public URLName getURLName();
public Session getSession();
// Event handling
public void addConnectionListener(ConnectionListener l);
public void removeConnectionListener(ConnectionListener l);
// String representation
public String toString();
}Jakarta Mail provides an authentication framework for secure connections.
public abstract class Authenticator {
// Override this method to provide authentication
protected PasswordAuthentication getPasswordAuthentication();
// Context information available during authentication
protected String getDefaultUserName();
protected String getRequestingPrompt();
protected InetAddress getRequestingSite();
protected int getRequestingPort();
protected String getRequestingProtocol();
protected String getRequestingScheme();
}
public final class PasswordAuthentication {
public PasswordAuthentication(String userName, String password);
public String getUserName();
public String getPassword();
}Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// Could prompt user, read from config, etc.
return new PasswordAuthentication("user@example.com", "password");
}
};
Session session = Session.getInstance(props, auth);URLName provides a structured way to represent mail server connection information.
public class URLName {
public URLName(String protocol, String host, int port, String file,
String username, String password);
public URLName(URL url);
public URLName(String url);
// Component access
public String getProtocol();
public String getHost();
public int getPort();
public String getFile();
public String getUsername();
public String getPassword();
public String getRef();
// URL operations
public URL getURL() throws MalformedURLException;
public String toString();
public boolean equals(Object obj);
public int hashCode();
}// Create URL name for IMAP connection
URLName url = new URLName("imaps", "imap.gmail.com", 993, null, "user@gmail.com", "password");
// Use with store
Store store = session.getStore(url);
store.connect();The Provider class describes available protocol implementations.
public class Provider {
public static final class Type {
public static final Type STORE;
public static final Type TRANSPORT;
public String toString();
}
public Provider(Type type, String protocol, String className, String vendor, String version);
public Type getType();
public String getProtocol();
public String getClassName();
public String getVendor();
public String getVersion();
public String toString();
}// Get available providers
Provider[] providers = session.getProviders();
for (Provider provider : providers) {
System.out.println("Protocol: " + provider.getProtocol());
System.out.println("Type: " + provider.getType());
System.out.println("Class: " + provider.getClassName());
}
// Get specific provider
Provider smtpProvider = session.getProvider("smtp");Install with Tessl CLI
npx tessl i tessl/maven-jakarta-mail--jakarta-mail-api