Skip navigation links

Package com.sun.mail.imap

An IMAP protocol provider for the JavaMail API that provides access to an IMAP message store.

See: Description

Package com.sun.mail.imap Description

An IMAP protocol provider for the JavaMail API that provides access to an IMAP message store. Both the IMAP4 and IMAP4rev1 protocols are supported. Refer to RFC 3501 for more information. The IMAP protocol provider also supports many IMAP extensions (described below). Note that the server needs to support these extensions (and not all servers do) in order to use the support in the IMAP provider. You can query the server for support of these extensions using the IMAPStore hasCapability method using the capability name defined by the extension (see the appropriate RFC) after connecting to the server.

UIDPLUS Support

The IMAP UIDPLUS extension (RFC 4315) is supported via the IMAPFolder methods addMessages, appendUIDMessages, and copyUIDMessages.

MOVE Support

The IMAP MOVE extension (RFC 6851) is supported via the IMAPFolder methods moveMessages and moveUIDMessages.

SASL Support

The IMAP protocol provider can use SASL (RFC 4422) authentication mechanisms on systems that support the javax.security.sasl APIs. The SASL-IR (RFC 4959) capability is also supported. In addition to the SASL mechanisms that are built into the SASL implementation, users can also provide additional SASL mechanisms of their own design to support custom authentication schemes. See the Java SASL API Programming and Deployment Guide for details. Note that the current implementation doesn't support SASL mechanisms that provide their own integrity or confidentiality layer.

OAuth 2.0 Support

Support for OAuth 2.0 authentication via the XOAUTH2 authentication mechanism is provided either through the SASL support described above or as a built-in authentication mechanism in the IMAP provider. The OAuth 2.0 Access Token should be passed as the password for this mechanism. See OAuth2 Support for details.

Connection Pool

A connected IMAPStore maintains a pool of IMAP protocol objects for use in communicating with the IMAP server. The IMAPStore will create the initial AUTHENTICATED connection and seed the pool with this connection. As folders are opened and new IMAP protocol objects are needed, the IMAPStore will provide them from the connection pool, or create them if none are available. When a folder is closed, its IMAP protocol object is returned to the connection pool if the pool is not over capacity.

A mechanism is provided for timing out idle connection pool IMAP protocol objects. Timed out connections are closed and removed (pruned) from the connection pool.

The connected IMAPStore object may or may not maintain a separate IMAP protocol object that provides the store a dedicated connection to the IMAP server. This is provided mainly for compatibility with previous implementations of the IMAP protocol provider.

QUOTA Support

The IMAP QUOTA extension (RFC 2087) is supported via the QuotaAwareStore interface implemented by IMAPStore, and the IMAPFolder getQuota and IMAPFolder setQuota methods. ACL Support

The IMAP ACL extension (RFC 2086) is supported via the Rights class and the IMAPFolder methods getACL, addACL, removeACL, addRights, removeRights, listRights, and myRights.

SORT Support

The IMAP SORT extension (RFC 5256) is supported via the SortTerm class and the IMAPFolder getSortedMessages methods.

CONDSTORE and QRESYNC Support

Basic support is provided for the IMAP CONDSTORE (RFC 4551) and QRESYNC (RFC 5162) extensions for the purpose of resynchronizing a folder after offline operation. Of course, the server must support these extensions. Use of these extensions is enabled by using the new IMAPFolder open method and supplying an appropriate ResyncData instance. Using ResyncData.CONDSTORE enables the CONDSTORE extension, which allows you to discover the modification sequence number (modseq) of messages using the IMAPMessage getModSeq method and the IMAPFolder getHighestModSeq method. Using a ResyncData instance with appropriate values also allows the server to report any changes in messages since the last resynchronization. The changes are reported as a list of MailEvent instances. The special MessageVanishedEvent reports on UIDs of messages that have been removed since the last resynchronization. A MessageChangedEvent reports on changes to flags of messages. For example:

        Folder folder = store.getFolder("whatever");
        IMAPFolder ifolder = (IMAPFolder)folder;
        List<MailEvent> events = ifolder.open(Folder.READ_WRITE,
                    new ResyncData(prevUidValidity, prevModSeq));
        for (MailEvent ev : events) {
            if (ev instanceOf MessageChangedEvent) {
                // process flag changes
            } else if (ev instanceof MessageVanishedEvent) {
                // process messages that were removed
            }
        }

See the referenced RFCs for more details on these IMAP extensions.

WITHIN Search Support

The IMAP WITHIN search extension (RFC 5032) is supported via the YoungerTerm and OlderTerm SearchTerms, which can be used as follows:

        // search for messages delivered in the last day
        Message[] msgs = folder.search(new YoungerTerm(24 * 60 * 60));
LOGIN-REFERRAL Support

The IMAP LOGIN-REFERRAL extension (RFC 2221) is supported. If a login referral is received when connecting or when authentication fails, a ReferralException is thrown. A referral can also occur when login succeeds. By default, no exception is thrown in this case. To force an exception to be thrown and the authentication to fail, set the mail.imap.referralexception property to "true".

COMPRESS Support

The IMAP COMPRESS extension (RFC 4978) is supported. If the server supports the extension and the mail.imap.compress.enable property is set to "true", compression will be enabled.

UTF-8 Support

The IMAP UTF8 extension (RFC 6855) is supported. If the server supports the extension, the client will enable use of UTF-8, allowing use of UTF-8 in IMAP protocol strings such as folder names.

Properties

The IMAP protocol provider supports the following properties, which may be set in the JavaMail Session object. The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

        props.put("mail.imap.port", "888");

to set the mail.imap.port property, which is of type int.

Note that if you're using the "imaps" protocol to access IMAP over SSL, all the properties would be named "mail.imaps.*".

Name Type Description
mail.imap.user String Default user name for IMAP.
mail.imap.host String The IMAP server to connect to.
mail.imap.port int The IMAP server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 143.
mail.imap.partialfetch boolean Controls whether the IMAP partial-fetch capability should be used. Defaults to true.
mail.imap.fetchsize int Partial fetch size in bytes. Defaults to 16K.
mail.imap.peek boolean If set to true, use the IMAP PEEK option when fetching body parts, to avoid setting the SEEN flag on messages. Defaults to false. Can be overridden on a per-message basis by the setPeek method on IMAPMessage.
mail.imap.ignorebodystructuresize boolean The IMAP BODYSTRUCTURE response includes the exact size of each body part. Normally, this size is used to determine how much data to fetch for each body part. Some servers report this size incorrectly in some cases; this property can be set to work around such server bugs. If this property is set to true, this size is ignored and data is fetched until the server reports the end of data. This will result in an extra fetch if the data size is a multiple of the block size. Defaults to false.
mail.imap.connectiontimeout int Socket connection timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.
mail.imap.timeout int Socket read timeout value in milliseconds. This timeout is implemented by java.net.Socket. Default is infinite timeout.
mail.imap.writetimeout int Socket write timeout value in milliseconds. This timeout is implemented by using a java.util.concurrent.ScheduledExecutorService per connection that schedules a thread to close the socket if the timeout expires. Thus, the overhead of using this timeout is one thread per connection. Default is infinite timeout.
mail.imap.statuscachetimeout int Timeout value in milliseconds for cache of STATUS command response. Default is 1000 (1 second). Zero disables cache.
mail.imap.appendbuffersize int Maximum size of a message to buffer in memory when appending to an IMAP folder. If not set, or set to -1, there is no maximum and all messages are buffered. If set to 0, no messages are buffered. If set to (e.g.) 8192, messages of 8K bytes or less are buffered, larger messages are not buffered. Buffering saves cpu time at the expense of short term memory usage. If you commonly append very large messages to IMAP mailboxes you might want to set this to a moderate value (1M or less).
mail.imap.connectionpoolsize int Maximum number of available connections in the connection pool. Default is 1.
mail.imap.connectionpooltimeout int Timeout value in milliseconds for connection pool connections. Default is 45000 (45 seconds).
mail.imap.separatestoreconnection boolean Flag to indicate whether to use a dedicated store connection for store commands. Default is false.
mail.imap.allowreadonlyselect boolean If false, attempts to open a folder read/write will fail if the SELECT command succeeds but indicates that the folder is READ-ONLY. This sometimes indicates that the folder contents can'tbe changed, but the flags are per-user and can be changed, such as might be the case for public shared folders. If true, such open attempts will succeed, allowing the flags to be changed. The getMode method on the Folder object will return Folder.READ_ONLY in this case even though the open method specified Folder.READ_WRITE. Default is false.
mail.imap.auth.mechanisms String If set, lists the authentication mechanisms to consider, and the order in which to consider them. Only mechanisms supported by the server and supported by the current implementation will be used. The default is "PLAIN LOGIN NTLM", which includes all the authentication mechanisms supported by the current implementation except XOAUTH2.
mail.imap.auth.login.disable boolean If true, prevents use of the non-standard AUTHENTICATE LOGIN command, instead using the plain LOGIN command. Default is false.
mail.imap.auth.plain.disable boolean If true, prevents use of the AUTHENTICATE PLAIN command. Default is false.
mail.imap.auth.ntlm.disable boolean If true, prevents use of the AUTHENTICATE NTLM command. Default is false.
mail.imap.auth.ntlm.domain String The NTLM authentication domain.
mail.imap.auth.ntlm.flags int NTLM protocol-specific flags. See http://curl.haxx.se/rfc/ntlm.html#theNtlmFlags for details.
mail.imap.auth.xoauth2.disable boolean If true, prevents use of the AUTHENTICATE XOAUTH2 command. Because the OAuth 2.0 protocol requires a special access token instead of a password, this mechanism is disabled by default. Enable it by explicitly setting this property to "false" or by setting the "mail.imap.auth.mechanisms" property to "XOAUTH2".
mail.imap.proxyauth.user String If the server supports the PROXYAUTH extension, this property specifies the name of the user to act as. Authenticate to the server using the administrator's credentials. After authentication, the IMAP provider will issue the PROXYAUTH command with the user name specified in this property.
mail.imap.localaddress String Local address (host name) to bind to when creating the IMAP socket. Defaults to the address picked by the Socket class. Should not normally need to be set, but useful with multi-homed hosts where it's important to pick a particular local address to bind to.
mail.imap.localport int Local port number to bind to when creating the IMAP socket. Defaults to the port number picked by the Socket class.
mail.imap.sasl.enable boolean If set to true, attempt to use the javax.security.sasl package to choose an authentication mechanism for login. Defaults to false.
mail.imap.sasl.mechanisms String A space or comma separated list of SASL mechanism names to try to use.
mail.imap.sasl.authorizationid String The authorization ID to use in the SASL authentication. If not set, the authentication ID (user name) is used.
mail.imap.sasl.realm String The realm to use with SASL authentication mechanisms that require a realm, such as DIGEST-MD5.
mail.imap.sasl.usecanonicalhostname boolean If set to true, the canonical host name returned by InetAddress.getCanonicalHostName is passed to the SASL mechanism, instead of the host name used to connect. Defaults to false.
mail.imap.sasl. xgwtrustedapphack.enable boolean If set to true, enables a workaround for a bug in the Novell Groupwise XGWTRUSTEDAPP SASL mechanism, when that mechanism is being used. Defaults to true.
mail.imap.socketFactory SocketFactory If set to a class that implements the javax.net.SocketFactory interface, this class will be used to create IMAP sockets. Note that this is an instance of a class, not a name, and must be set using the put method, not the setProperty method.
mail.imap.socketFactory.class String If set, specifies the name of a class that implements the javax.net.SocketFactory interface. This class will be used to create IMAP sockets.
mail.imap.socketFactory.fallback boolean If set to true, failure to create a socket using the specified socket factory class will cause the socket to be created using the java.net.Socket class. Defaults to true.
mail.imap.socketFactory.port int Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.imap.usesocketchannels boolean If set to true, use SocketChannels instead of Sockets for connecting to the server. Required if using the IdleManager. Ignored if a socket factory is set. Defaults to false.
mail.imap.ssl.enable boolean If set to true, use SSL to connect and use the SSL port by default. Defaults to false for the "imap" protocol and true for the "imaps" protocol.
mail.imap.ssl.checkserveridentity boolean If set to true, check the server identity as specified by RFC 2595. These additional checks based on the content of the server's certificate are intended to prevent man-in-the-middle attacks. Defaults to false.
mail.imap.ssl.trust String If set, and a socket factory hasn't been specified, enables use of a MailSSLSocketFactory. If set to "*", all hosts are trusted. If set to a whitespace separated list of hosts, those hosts are trusted. Otherwise, trust depends on the certificate the server presents.
mail.imap.ssl.socketFactory SSLSocketFactory If set to a class that extends the javax.net.ssl.SSLSocketFactory class, this class will be used to create IMAP SSL sockets. Note that this is an instance of a class, not a name, and must be set using the put method, not the setProperty method.
mail.imap.ssl.socketFactory.class String If set, specifies the name of a class that extends the javax.net.ssl.SSLSocketFactory class. This class will be used to create IMAP SSL sockets.
mail.imap.ssl.socketFactory.port int Specifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.imap.ssl.protocols string Specifies the SSL protocols that will be enabled for SSL connections. The property value is a whitespace separated list of tokens acceptable to the javax.net.ssl.SSLSocket.setEnabledProtocols method.
mail.imap.ssl.ciphersuites string Specifies the SSL cipher suites that will be enabled for SSL connections. The property value is a whitespace separated list of tokens acceptable to the javax.net.ssl.SSLSocket.setEnabledCipherSuites method.
mail.imap.starttls.enable boolean If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. If the server does not support STARTTLS, the connection continues without the use of TLS; see the mail.imap.starttls.required property to fail if STARTTLS isn't supported. Note that an appropriate trust store must configured so that the client will trust the server's certificate. Default is false.
mail.imap.starttls.required boolean If true, requires the use of the STARTTLS command. If the server doesn't support the STARTTLS command, or the command fails, the connect method will fail. Defaults to false.
mail.imap.proxy.host string Specifies the host name of an HTTP web proxy server that will be used for connections to the mail server.
mail.imap.proxy.port string Specifies the port number for the HTTP web proxy server. Defaults to port 80.
mail.imap.proxy.user string Specifies the user name to use to authenticate with the HTTP web proxy server. By default, no authentication is done.
mail.imap.proxy.password string Specifies the password to use to authenticate with the HTTP web proxy server. By default, no authentication is done.
mail.imap.socks.host string Specifies the host name of a SOCKS5 proxy server that will be used for connections to the mail server.
mail.imap.socks.port string Specifies the port number for the SOCKS5 proxy server. This should only need to be used if the proxy server is not using the standard port number of 1080.
mail.imap.minidletime int Applications typically call the idle method in a loop. If another thread termiantes the IDLE command, it needs a chance to do its work before another IDLE command is issued. The idle method enforces a delay to prevent thrashing between the IDLE command and regular commands. This property sets the delay in milliseconds. If not set, the default is 10 milliseconds.
mail.imap.enableresponseevents boolean Enable special IMAP-specific events to be delivered to the Store's ConnectionListener. If true, IMAP OK, NO, BAD, or BYE responses will be sent as ConnectionEvents with a type of IMAPStore.RESPONSE. The event's message will be the raw IMAP response string. By default, these events are not sent. NOTE: This capability is highly experimental and likely will change in future releases.
mail.imap.enableimapevents boolean Enable special IMAP-specific events to be delivered to the Store's ConnectionListener. If true, unsolicited responses received during the Store's idle method will be sent as ConnectionEvents with a type of IMAPStore.RESPONSE. The event's message will be the raw IMAP response string. By default, these events are not sent. NOTE: This capability is highly experimental and likely will change in future releases.
mail.imap.throwsearchexception boolean If set to true and a SearchTerm passed to the Folder.search method is too complex for the IMAP protocol, throw a SearchException. For example, the IMAP protocol only supports less-than and greater-than comparisons for a SizeTerm. If false, the search will be done locally by fetching the required message data and comparing it locally. Defaults to false.
mail.imap.folder.class String Class name of a subclass of com.sun.mail.imap.IMAPFolder. The subclass can be used to provide support for additional IMAP commands. The subclass must have public constructors of the form public MyIMAPFolder(String fullName, char separator, IMAPStore store, Boolean isNamespace) and public MyIMAPFolder(ListInfo li, IMAPStore store)
mail.imap.closefoldersonstorefailure boolean In some cases, a failure of the Store connection indicates a failure of the server, and all Folders associated with that Store should also be closed. In other cases, a Store connection failure may be a transient failure, and Folders may continue to operate normally. If this property is true (the default), failures in the Store connection cause all associated Folders to be closed. Set this property to false to better handle transient failures in the Store connection.
mail.imap.finalizecleanclose boolean When the finalizer for IMAPStore is called, should the connection to the server be closed cleanly, as if the application called the close method? Or should the connection to the server be closed without sending any commands to the server? Defaults to false, the connection is not closed cleanly.
mail.imap.referralexception boolean If set to true and an IMAP login referral is returned when the authentication succeeds, fail the connect request and throw a ReferralException. Defaults to false.
mail.imap.compress.enable boolean If set to true and the IMAP server supports the COMPRESS=DEFLATE extension, compression will be enabled. Defaults to false.
mail.imap.compress.level int The compression level to be used, in the range -1 to 9. See the Deflater class for details.
mail.imap.compress.strategy int The compression strategy to be used, in the range 0 to 2. See the Deflater class for details.
mail.imap.compress.strategy boolean If true, always use "A" for the IMAP command tag prefix. If false, the IMAP command tag prefix is different for each connection, from "A" through "ZZZ" and then wrapping around to "A". Applications should never need to set this. Defaults to false.

In general, applications should not need to use the classes in this package directly. Instead, they should use the APIs defined by the javax.mail package (and subpackages). Applications should never construct instances of IMAPStore or IMAPFolder directly. Instead, they should use the Session method getStore to acquire an appropriate Store object, and from that acquire Folder objects.

Loggers

In addition to printing debugging output as controlled by the Session configuration, the com.sun.mail.imap provider logs the same information using Logger as described in the following table:

Logger Name Logging Level Purpose
com.sun.mail.imap CONFIG Configuration of the IMAPStore
com.sun.mail.imap FINE General debugging output
com.sun.mail.imap.connectionpool CONFIG Configuration of the IMAP connection pool
com.sun.mail.imap.connectionpool FINE Debugging output related to the IMAP connection pool
com.sun.mail.imap.messagecache CONFIG Configuration of the IMAP message cache
com.sun.mail.imap.messagecache FINE Debugging output related to the IMAP message cache
com.sun.mail.imap.protocol FINEST Complete protocol trace
WARNING

WARNING: The APIs unique to this package should be considered EXPERIMENTAL. They may be changed in the future in ways that are incompatible with applications using the current APIs.

Skip navigation links

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