JavaMail API - IMAP Servers

IMAP is Acronym for Internet Message Access Protocol. It is an Application Layer Internet protocol that allows an e-mail client to access e-mail on a remote mail server. An IMAP server typically listens on well-known port 143. IMAP over SSL (IMAPS) is assigned to port number 993.

IMAP supports both on-line and off-line modes of operation. E-mail clients using IMAP generally leave messages on the server until the user explicitly deletes them.

Package com.sun.mail.imap is an IMAP protocol provider for the JavaMail API that provides access to an IMAP message store. The table below lists the interface and classes of this provider:

IMAPFolder.ProtocolCommandThis a simple interface for user-defined IMAP protocol commands.
ACLThis is a class. An access control list entry for a particular authentication identifier (user or group).
IMAPFolderThis class implements an IMAP folder.
IMAPFolder.FetchProfileItemThis a class for fetching headers.
IMAPMessageThis class implements an ReadableMime object.
IMAPMessage.FetchProfileCondition This class implements the test to be done on each message in the folder.
IMAPSSLStoreThis class provides access to an IMAP message store over SSL.
IMAPStoreThis class provides access to an IMAP message store.
RightsThis class represents the set of rights for an authentication identifier (for instance, a user or a group).
Rights.Right This inner class represents an individual right.
SortTermA particular sort criteria, as defined by RFC 5256.

Some points to be noted above this provider:

  • This provider supports both the IMAP4 and IMAP4rev1 protocols.

  • A connected IMAPStore maintains a pool of IMAP protocol objects for use in communicating with the IMAP server. 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 .

  • 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.

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.

mail.imap.userStringDefault user name for IMAP.
mail.imap.hostStringThe IMAP server to connect to.
mail.imap.portintThe IMAP server port to connect to, if the connect() method doesn't explicitly specify one. Defaults to 143.
mail.imap.partialfetch booleanControls whether the IMAP partial-fetch capability should be used. Defaults to true.
mail.imap.fetchsizeintPartial fetch size in bytes. Defaults to 16K.
mail.imap.ignorebodystructuresize booleanThe 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. Defaults to false.
mail.imap.connectiontimeoutintSocket connection timeout value in milliseconds. Default is infinite timeout.
mail.imap.timeout intSocket I/O timeout value in milliseconds. Default is infinite timeout.
mail.imap.statuscachetimeoutintTimeout value in milliseconds for cache of STATUS command response. Default is 1000 (1 second). Zero disables cache.
mail.imap.appendbuffersizeintMaximum size of a message to buffer in memory when appending to an IMAP folder.
mail.imap.connectionpoolsize intMaximum number of available connections in the connection pool. Default is 1.
mail.imap.connectionpooltimeoutintTimeout value in milliseconds for connection pool connections. Default is 45000 (45 seconds).
mail.imap.separatestoreconnection booleanFlag to indicate whether to use a dedicated store connection for store commands. Default is false.
mail.imap.auth.login.disablebooleanIf true, prevents use of the non-standard AUTHENTICATE LOGIN command, instead using the plain LOGIN command. Default is false.
mail.imap.auth.plain.disable booleanIf true, prevents use of the AUTHENTICATE PLAIN command. Default is false.
mail.imap.auth.ntlm.disablebooleanIf true, prevents use of the AUTHENTICATE NTLM command. Default is false.
mail.imap.proxyauth.userStringIf 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.localaddressStringLocal address (host name) to bind to when creating the IMAP socket. Defaults to the address picked by the Socket class.
mail.imap.localport intLocal port number to bind to when creating the IMAP socket. Defaults to the port number picked by the Socket class.
mail.imap.sasl.enablebooleanIf set to true, attempt to use the package to choose an authentication mechanism for login. Defaults to false.
mail.imap.sasl.mechanismsStringA space or comma separated list of SASL mechanism names to try to use.
mail.imap.sasl.authorizationidStringThe authorization ID to use in the SASL authentication. If not set, the authentication ID (user name) is used.
mail.imap.sasl.realmStringThe realm to use with SASL authentication mechanisms that require a realm, such as DIGEST-MD5.
mail.imap.auth.ntlm.domainStringThe NTLM authentication domain.
mail.imap.auth.ntlm.flags intNTLM protocol-specific flags.
mail.imap.socketFactory Socket FactoryIf set to a class that implements the interface, this class will be used to create IMAP sockets.
mail.imap.socketFactory.classStringIf set, specifies the name of a class that implements the interface. This class will be used to create IMAP sockets.
mail.imap.socketFactory.fallbackbooleanIf set to true, failure to create a socket using the specified socket factory class will cause the socket to be created using the class. Defaults to true.
mail.imap.socketFactory.portintSpecifies the port to connect to when using the specified socket factory. Default port is used when not set.
mail.imap.ssl.enablebooleanIf 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.checkserveridentitybooleanIf set to true, check the server identity as specified by RFC 2595. Defaults to false.
mail.imap.ssl.trustStringIf 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 SSL Socket FactoryIf set to a class that extends the class, this class will be used to create IMAP SSL sockets.
mail.imap.ssl.socketFactory.classStringIf set, specifies the name of a class that extends the class. This class will be used to create IMAP SSL sockets.
mail.imap.ssl.socketFactory.portintSpecifies the port to connect to when using the specified socket factory. If not set, the default port will be used.
mail.imap.ssl.protocols stringSpecifies the SSL protocols that will be enabled for SSL connections. The property value is a whitespace separated list of tokens acceptable to the method.
mail.imap.starttls.enable booleanIf 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. Default is false.
mail.imap.starttls.required booleanIf 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. stringSpecifies the host name of a SOCKS5 proxy server that will be used for connections to the mail server.
mail.imap.socks.portstringSpecifies 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.minidletimeint This property sets the delay in milliseconds. If not set, the default is 10 milliseconds.
mail.imap.enableimapevents booleanEnable 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.
mail.imap.folder.classStringClass 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)

In general, applications should not need to use the classes in this package directly. Instead, they should use the APIs defined by 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.

Examples to use IMAP server is demonstrated in chapter Quota Management.