Jakarta Mail API provides a platform-independent and protocol-independent framework to build mail and messaging applications
npx @tessl/cli install tessl/maven-com-sun-mail--jakarta-mail-api@1.6.0Jakarta Mail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. It offers comprehensive support for SMTP, IMAP, POP3 protocols with MIME message handling, folder operations, search capabilities, and event-driven programming.
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>jakarta.mail</artifactId>
<version>1.6.7</version>
</dependency>import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import javax.mail.event.*;
import java.util.Properties;
import javax.activation.DataHandler;// Create session
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.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", "password");
}
});
// Create and send message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender@example.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("recipient@example.com"));
message.setSubject("Hello World");
message.setText("This is a test message.");
Transport.send(message);
// Read messages
Store store = session.getStore("imap");
store.connect("imap.example.com", "username", "password");
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message[] messages = inbox.getMessages();The Jakarta Mail API follows a provider-based architecture with these key components:
Session management, message creation, transport operations, and store connectivity.
class Session {
// Static factory methods
public static Session getInstance(Properties props);
public static Session getInstance(Properties props, Authenticator authenticator);
public static synchronized Session getDefaultInstance(Properties props);
public static synchronized Session getDefaultInstance(Properties props, Authenticator authenticator);
// Store and Transport 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(Provider provider) throws NoSuchProviderException;
public Transport getTransport(Address address) throws NoSuchProviderException;
// Folder access
public Folder getFolder(URLName url) throws MessagingException;
// Properties and configuration
public Properties getProperties();
public String getProperty(String name);
// Provider management
public synchronized Provider[] getProviders();
public synchronized Provider getProvider(String protocol) throws NoSuchProviderException;
public synchronized void setProvider(Provider provider) throws NoSuchProviderException;
public synchronized void addProvider(Provider provider);
// Authentication
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);
// Debug support
public synchronized void setDebug(boolean debug);
public synchronized boolean getDebug();
public synchronized void setDebugOut(PrintStream out);
public synchronized PrintStream getDebugOut();
// Protocol configuration
public synchronized void setProtocolForAddress(String addresstype, String protocol);
}
abstract class Message implements Part {
// 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 abstract void setRecipients(RecipientType type, Address[] addresses) throws MessagingException;
public abstract void addRecipients(RecipientType type, 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;
// Flag management
public abstract Flags getFlags() throws MessagingException;
public abstract void setFlags(Flags flag, boolean set) throws MessagingException;
// Operations
public abstract Message reply(boolean replyToAll) throws MessagingException;
public abstract void saveChanges() throws MessagingException;
}
abstract class Transport extends Service {
// Static send 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;
}MIME message handling, multipart content, address parsing, and encoding utilities.
class MimeMessage extends Message implements MimePart {
public MimeMessage(Session session);
public MimeMessage(Session session, InputStream is) throws MessagingException;
public MimeMessage(MimeMessage source) throws MessagingException;
public void setText(String text) throws MessagingException;
public void setContent(Object o, String type) throws MessagingException;
public void setContent(Multipart mp) throws MessagingException;
public Object getContent() throws IOException, MessagingException;
public String getContentType() throws MessagingException;
public void setHeader(String name, String value) throws MessagingException;
public String[] getHeader(String name) throws MessagingException;
public void addHeader(String name, String value) throws MessagingException;
public void removeHeader(String name) throws MessagingException;
public String getMessageID() throws MessagingException;
public void updateHeaders() throws MessagingException;
}
class InternetAddress extends Address {
public InternetAddress();
public InternetAddress(String address) throws AddressException;
public InternetAddress(String address, String personal) throws AddressException;
public InternetAddress(String address, String personal, String charset) throws UnsupportedEncodingException;
public String getAddress();
public void setAddress(String address);
public String getPersonal();
public void setPersonal(String name) throws UnsupportedEncodingException;
public static InternetAddress[] parse(String addresslist) throws AddressException;
public static InternetAddress[] parse(String addresslist, boolean strict) throws AddressException;
public static InternetAddress[] parseHeader(String addresslist, boolean strict) throws AddressException;
public void validate() throws AddressException;
public boolean isGroup();
public InternetAddress[] getGroup(boolean strict) throws AddressException;
}
class MimeMultipart extends Multipart {
public MimeMultipart();
public MimeMultipart(String subtype) throws MessagingException;
public MimeMultipart(DataSource ds) throws MessagingException;
public void addBodyPart(BodyPart part) throws MessagingException;
public void addBodyPart(BodyPart part, int index) throws MessagingException;
public BodyPart getBodyPart(int index) throws MessagingException;
public boolean removeBodyPart(BodyPart part) throws MessagingException;
public int getCount() throws MessagingException;
public String getPreamble() throws MessagingException;
public void setPreamble(String preamble) throws MessagingException;
}Folder management, message storage, flag operations, and hierarchical navigation.
abstract class Folder implements AutoCloseable {
abstract void open(int mode) throws MessagingException;
abstract Message[] getMessages() throws MessagingException;
abstract void appendMessages(Message[] msgs) throws MessagingException;
abstract Message[] expunge() throws MessagingException;
abstract Folder[] list(String pattern) throws MessagingException;
}
abstract class Store extends Service {
abstract Folder getDefaultFolder() throws MessagingException;
abstract Folder getFolder(String name) throws MessagingException;
}Message searching with complex criteria, logical operators, and content filtering.
abstract class SearchTerm implements Serializable {
abstract boolean match(Message msg);
}
class AndTerm extends SearchTerm {
AndTerm(SearchTerm t1, SearchTerm t2);
AndTerm(SearchTerm[] terms);
}
class SubjectTerm extends StringTerm {
SubjectTerm(String pattern);
}
class FromTerm extends AddressTerm {
FromTerm(Address address);
}Mail event listeners, connection monitoring, and message change notifications.
interface MessageCountListener extends EventListener {
void messagesAdded(MessageCountEvent e);
void messagesRemoved(MessageCountEvent e);
}
interface ConnectionListener extends EventListener {
void opened(ConnectionEvent e);
void closed(ConnectionEvent e);
void disconnected(ConnectionEvent e);
}
abstract class MessageCountAdapter implements MessageCountListener {
// Empty implementations for optional override
}class Properties extends Hashtable<Object,Object> {
String getProperty(String key);
Object setProperty(String key, String value);
}
abstract class Authenticator {
protected PasswordAuthentication getPasswordAuthentication();
}
class PasswordAuthentication {
PasswordAuthentication(String userName, String password);
String getUserName();
String getPassword();
}
class MessagingException extends Exception {
MessagingException();
MessagingException(String s);
MessagingException(String s, Exception e);
}
class Flags implements Cloneable, Serializable {
Flags();
Flags(Flag flag);
void add(Flag flag);
boolean contains(Flag flag);
static class Flag {
static final Flag ANSWERED;
static final Flag DELETED;
static final Flag DRAFT;
static final Flag FLAGGED;
static final Flag RECENT;
static final Flag SEEN;
}
}