Overview (JavaMail API documentation)
Date: 2019-08-31

# JavaMail API documentation

The JavaMailTM API provides classes that model a mail system.

See: Description

JavaMail API Packages
Package | Description
--- | ---
javax.mail | The JavaMailTM API provides classes that model a mail system.
javax.mail.event | Listeners and events for the JavaMail API.
javax.mail.internet | Classes specific to Internet mail systems.
javax.mail.search | Message search terms for the JavaMail API.
javax.mail.util | JavaMail API utility classes.

Sun-specific Packages
Package | Description
--- | ---
com.sun.mail.dsn | Support for creating and parsing Delivery Status Notifications.
com.sun.mail.imap | An IMAP protocol provider for the JavaMail API that provides access to an IMAP message store.
com.sun.mail.pop3 | A POP3 protocol provider for the JavaMail API that provides access to a POP3 message store.
com.sun.mail.smtp | An SMTP protocol provider for the JavaMail API that provides access to an SMTP server.
com.sun.mail.util | Utility classes for use with the JavaMail API.
com.sun.mail.util.logging | Contains JavaMailTM extensions for the JavaTM platform's core logging facilities.

The JavaMailTM API provides classes that model a mail system. The `javax.mail` package defines classes that are common to all mail systems. The `javax.mail.internet` package defines classes that are specific to mail systems based on internet standards such as MIME, SMTP, POP3, and IMAP. The JavaMail API includes the `javax.mail` package and subpackages.

For an overview of the JavaMail API, read the JavaMail specification included in the download bundle or available on the JavaMail web site.

The code to send a plain text message can be as simple as the following:

        Properties props = new Properties();
        props.put("mail.smtp.host", "my-mail-server");
        Session session = Session.getInstance(props, null);

        try {
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom("me@example.com");
            msg.setRecipients(Message.RecipientType.TO,
                              "you@example.com");
            msg.setSubject("JavaMail hello world example");
            msg.setSentDate(new Date());
            msg.setText("Hello, world!\n");
            Transport.send(msg, "me@example.com", "my-password");
        } catch (MessagingException mex) {
            System.out.println("send failed, exception: " + mex);
        }

The JavaMail download bundle contains many more complete examples in the "demo" directory.

Don't forget to see the JavaMail API FAQ for answers to the most common questions. The JavaMail web site contains many additional resources.

The JavaMail API supports the following standard properties, which may be set in the `Session` object, or in the `Properties` object used to create the `Session` object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

            props.put("mail.debug", "true");

to set the `mail.debug` property, which is of type boolean.

Name | Type | Description
--- | --- | ---
mail.debug | boolean | The initial debug mode. Default is false.
mail.from | String | The return email address of the current user, used by the `InternetAddress` method `getLocalAddress`.
mail.mime.address.strict | boolean | The MimeMessage class uses the `InternetAddress` method `parseHeader` to parse headers in messages. This property controls the strict flag passed to the `parseHeader` method. The default is true.
mail.host | String | The default host name of the mail server for both Stores and Transports. Used if the `mail.protocol.host` property isn't set.
mail.store.protocol | String | Specifies the default message access protocol. The `Session` method `getStore()` returns a Store object that implements this protocol. By default the first Store provider in the configuration files is returned.
mail.transport.protocol | String | Specifies the default message transport protocol. The `Session` method `getTransport()` returns a Transport object that implements this protocol. By default the first Transport provider in the configuration files is returned.
mail.user | String | The default user name to use when connecting to the mail server. Used if the `mail.protocol.user` property isn't set.
mail.protocol.class | String | Specifies the fully qualified class name of the provider for the specified protocol. Used in cases where more than one provider for a given protocol exists; this property can be used to specify which provider to use by default. The provider must still be listed in a configuration file.
mail.protocol.host | String | The host name of the mail server for the specified protocol. Overrides the `mail.host` property.
mail.protocol.port | int | The port number of the mail server for the specified protocol. If not specified the protocol's default port number is used.
mail.protocol.user | String | The user name to use when connecting to mail servers using the specified protocol. Overrides the `mail.user` property.

The following properties are supported by the reference implementation (RI) of JavaMail, but are not currently a required part of the specification. The names, types, defaults, and semantics of these properties may change in future releases.

Name | Type | Description
--- | --- | ---
mail.debug.auth | boolean | Include protocol authentication commands (including usernames and passwords) in the debug output. Default is false.
mail.transport.protocol.address-type | String | Specifies the default message transport protocol for the specified address type. The `Session` method `getTransport(Address)` returns a Transport object that implements this protocol when the address is of the specified type (e.g., "rfc822" for standard internet addresses). By default the first Transport configured for that address type is used. This property can be used to override the behavior of the `send` method of the `Transport` class so that (for example) the "smtps" protocol is used instead of the "smtp" protocol by setting the property `mail.transport.protocol.rfc822` to `"smtps"`.

The JavaMail API also supports several System properties; see the `javax.mail.internet` package documentation for details.

The JavaMail reference implementation includes protocol providers in subpackages of `com.sun.mail`. Note that the APIs to these protocol providers are not part of the standard JavaMail API. Portable programs will not use these APIs.

Nonportable programs may use the APIs of the protocol providers by (for example) casting a returned `Folder` object to a `com.sun.mail.imap.IMAPFolder` object. Similarly for `Store` and `Message` objects returned from the standard JavaMail APIs.

The protocol providers also support properties that are specific to those providers. The package documentation for the `IMAP`, `POP3`, and `SMTP` packages provide details.

Copyright © 1996-2013, Oracle and/or its affiliates. All Rights Reserved. Use is subject to license terms< /a>.