public class IMAPConnection extends java.lang.Object implements IMAPConstants
Modifier and Type | Field and Description |
---|---|
protected java.util.List |
asyncResponses
List of responses received asynchronously.
|
protected static int |
DEFAULT_PORT
The default IMAP port.
|
protected static int |
DEFAULT_SSL_PORT
The default IMAP-SSL port.
|
static java.util.logging.Level |
IMAP_TRACE
The network trace level.
|
protected IMAPResponseTokenizer |
in
The tokenizer used to read IMAP responses from.
|
static java.util.logging.Logger |
logger
The logger used for IMAP protocol traces.
|
protected CRLFOutputStream |
out
The output stream.
|
protected java.net.Socket |
socket
The socket used for communication with the server.
|
protected static java.lang.String |
TAG_PREFIX
Prefix for tags.
|
protected static java.lang.String |
US_ASCII
The encoding used to create strings for IMAP commands.
|
ACL, ALERT, APPEND, APPENDUID, AUTHENTICATE, BAD, BODY, BODY_PEEK, BODYSTRUCTURE, BYE, CAPABILITY, CHECK, CLOSE, COPY, COPYUID, CRAM_MD5, CREATE, DELETE, DELETEACL, ENVELOPE, EXAMINE, EXISTS, EXPUNGE, FETCH, FETCH_FLAGS, FLAG_ANSWERED, FLAG_DELETED, FLAG_DRAFT, FLAG_FLAGGED, FLAG_RECENT, FLAG_SEEN, FLAGS, GETACL, GETQUOTA, GETQUOTAROOT, GSSAPI, HEADER, HEADER_FIELDS, HEADER_FIELDS_NOT, INTERNALDATE, KERBEROS_V4, LIST, LIST_MARKED, LIST_NOINFERIORS, LIST_NOSELECT, LIST_UNMARKED, LISTRIGHTS, LOGIN, LOGINDISABLED, LOGOUT, LSUB, MESSAGES, MYRIGHTS, NAMESPACE, NEWNAME, NIL, NO, NOOP, OK, PARSE, PERMANENTFLAGS, PREAUTH, QUOTA, QUOTAROOT, READ_ONLY, READ_WRITE, RECENT, RENAME, RFC822, RFC822_HEADER, RFC822_SIZE, RFC822_TEXT, RIGHTS_ADMIN, RIGHTS_CREATE, RIGHTS_DELETE, RIGHTS_INSERT, RIGHTS_LOOKUP, RIGHTS_POST, RIGHTS_READ, RIGHTS_SEEN, RIGHTS_WRITE, SEARCH, SEARCH_ALL, SEARCH_ANSWERED, SEARCH_BCC, SEARCH_BEFORE, SEARCH_BODY, SEARCH_CC, SEARCH_DELETED, SEARCH_DRAFT, SEARCH_FLAGGED, SEARCH_FROM, SEARCH_HEADER, SEARCH_KEYWORD, SEARCH_LARGER, SEARCH_NEW, SEARCH_NOT, SEARCH_OLD, SEARCH_ON, SEARCH_OR, SEARCH_RECENT, SEARCH_SEEN, SEARCH_SENTBEFORE, SEARCH_SENTON, SEARCH_SENTSINCE, SEARCH_SINCE, SEARCH_SMALLER, SEARCH_SUBJECT, SEARCH_TEXT, SEARCH_TO, SEARCH_UID, SEARCH_UNANSWERED, SEARCH_UNDELETED, SEARCH_UNDRAFT, SEARCH_UNFLAGGED, SEARCH_UNKEYWORD, SEARCH_UNSEEN, SELECT, SETACL, SETQUOTA, SKEY, STARTTLS, STATUS, STORAGE, STORE, SUBSCRIBE, TRYCREATE, UID, UID_EXPUNGE, UIDNEXT, UIDVALIDITY, UNSEEN, UNSUBSCRIBE
Constructor and Description |
---|
IMAPConnection(java.lang.String host)
Creates a new connection to the default IMAP port.
|
IMAPConnection(java.lang.String host,
int port)
Creates a new connection.
|
IMAPConnection(java.lang.String host,
int port,
int connectionTimeout,
int timeout)
Creates a new connection.
|
IMAPConnection(java.lang.String host,
int port,
int connectionTimeout,
int timeout,
boolean secure,
javax.net.ssl.TrustManager tm)
Creates a new connection.
|
IMAPConnection(java.lang.String host,
int port,
javax.net.ssl.TrustManager tm)
Creates a new secure connection using the specified trust manager.
|
Modifier and Type | Method and Description |
---|---|
boolean |
alertsPending()
Indicates if there are alerts pending for the user-agent.
|
boolean |
append(java.lang.String mailbox,
java.lang.String[] flags,
byte[] content)
Append a message to the specified mailbox.
|
boolean |
append(java.lang.String mailbox,
java.lang.String[] flags,
byte[] content,
UIDPlusHandler uidplus)
Append a message to the specified mailbox.
|
boolean |
authenticate(java.lang.String mechanism,
java.lang.String username,
java.lang.String password)
Authenticates the connection using the specified SASL mechanism,
username, and password.
|
java.util.List |
capability()
Returns a list of the capabilities of the IMAP server.
|
void |
check()
Request a checkpoint of the currently selected mailbox.
|
boolean |
close()
Permanently remove all messages that have the \Deleted flags set,
and close the mailbox.
|
boolean |
copy(int[] messages,
java.lang.String mailbox)
Copies the specified messages to the end of the destination mailbox.
|
boolean |
copy(int[] messages,
java.lang.String mailbox,
UIDPlusHandler uidplus)
Copies the specified messages to the end of the destination mailbox.
|
boolean |
create(java.lang.String mailbox)
Creates a mailbox with the specified name.
|
boolean |
delete(java.lang.String mailbox)
Deletes the mailbox with the specified name.
|
boolean |
deleteacl(java.lang.String mailbox,
java.lang.String principal)
Removes any access rights for the given authentication principal on the
specified mailbox.
|
MailboxStatus |
examine(java.lang.String mailbox)
Selects the specified mailbox.
|
int[] |
expunge()
Permanently removes all messages that have the \Delete flag set.
|
MessageStatus[] |
fetch(int[] messages,
java.lang.String[] fetchCommands)
Retrieves data associated with messages in the mailbox.
|
MessageStatus[] |
fetch(int start,
int end,
java.lang.String[] fetchCommands)
Retrieves data associated with the specified range of messages in
the mailbox.
|
MessageStatus |
fetch(int message,
java.lang.String[] fetchCommands)
Retrieves data associated with the specified message in the mailbox.
|
java.util.Map |
getacl(java.lang.String mailbox)
Returns the access control list for the specified mailbox.
|
java.lang.String[] |
getAlerts()
Returns the pending alerts for the user-agent as an array.
|
Quota |
getquota(java.lang.String quotaRoot)
Returns the specified quota root's resource usage and limits.
|
Quota[] |
getquotaroot(java.lang.String mailbox)
Returns the quotas for the given mailbox.
|
protected javax.net.ssl.SSLSocketFactory |
getSSLSocketFactory(javax.net.ssl.TrustManager tm)
Returns a configured SSLSocketFactory to use in creating new SSL
sockets.
|
boolean |
invokeSimpleCommand(java.lang.String command)
Sends the specified IMAP command.
|
ListEntry[] |
list(java.lang.String reference,
java.lang.String mailbox)
Returns a subset of names from the compete set of names available to
the client.
|
protected ListEntry[] |
listImpl(java.lang.String command,
java.lang.String reference,
java.lang.String mailbox) |
int |
listrights(java.lang.String mailbox,
java.lang.String principal)
Returns the rights for the given principal for the specified mailbox.
|
boolean |
login(java.lang.String username,
java.lang.String password)
Login to the connection using the username and password method.
|
void |
logout()
Logout this connection.
|
ListEntry[] |
lsub(java.lang.String reference,
java.lang.String mailbox)
Returns a subset of subscribed names.
|
int |
myrights(java.lang.String mailbox)
Returns the rights for the current principal for the specified mailbox.
|
Namespaces |
namespace()
Returns the namespaces available on the server.
|
protected java.lang.String |
newTag()
Returns a new tag for a command.
|
MailboxStatus |
noop()
Ping the server.
|
protected IMAPResponse |
readResponse()
Reads an IMAP response from the server.
|
boolean |
rename(java.lang.String source,
java.lang.String target)
Renames the source mailbox to the specified name.
|
int[] |
search(java.lang.String charset,
java.lang.String[] criteria)
Searches the currently selected mailbox for messages matching the
specified criteria.
|
MailboxStatus |
select(java.lang.String mailbox)
Selects the specified mailbox.
|
protected MailboxStatus |
selectImpl(java.lang.String mailbox,
java.lang.String command) |
protected void |
sendCommand(java.lang.String tag,
java.lang.String command)
Sends the specified IMAP tagged command to the server.
|
boolean |
setacl(java.lang.String mailbox,
java.lang.String principal,
int rights)
Changes the access rights on the specified mailbox such that the
authentication principal is granted the specified permissions.
|
void |
setAnsiDebug(boolean flag)
Sets whether debugging output should use ANSI colour escape sequences.
|
Quota |
setquota(java.lang.String quotaRoot,
Quota.Resource[] resources)
Sets the quota for the specified quota root.
|
boolean |
starttls()
Attempts to start TLS on the specified connection.
|
boolean |
starttls(javax.net.ssl.TrustManager tm)
Attempts to start TLS on the specified connection.
|
MailboxStatus |
status(java.lang.String mailbox,
java.lang.String[] statusNames)
Requests the status of the specified mailbox.
|
MessageStatus[] |
store(int[] messages,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with messages in the mailbox.
|
MessageStatus[] |
store(int start,
int end,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with the specified range of messages in the
mailbox.
|
MessageStatus |
store(int message,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with the specified message in the mailbox.
|
boolean |
subscribe(java.lang.String mailbox)
Adds the specified mailbox to the set of subscribed mailboxes as
returned by the LSUB command.
|
int[] |
uidExpunge(long start,
long end)
Expunges the specified range of messages.
|
MessageStatus[] |
uidFetch(long[] uids,
java.lang.String[] fetchCommands)
Retrieves data associated with messages in the mailbox.
|
MessageStatus[] |
uidFetch(long start,
long end,
java.lang.String[] fetchCommands)
Retrieves data associated with the specified range of messages in
the mailbox.
|
MessageStatus |
uidFetch(long uid,
java.lang.String[] fetchCommands)
Retrieves data associated with the specified message in the mailbox.
|
MessageStatus[] |
uidStore(long[] uids,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with messages in the mailbox.
|
MessageStatus[] |
uidStore(long start,
long end,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with the specified range of messages in the
mailbox.
|
MessageStatus |
uidStore(long uid,
java.lang.String flagCommand,
java.lang.String[] flags)
Alters data associated with the specified message in the mailbox.
|
boolean |
unsubscribe(java.lang.String mailbox)
Removes the specified mailbox from the set of subscribed mailboxes as
returned by the LSUB command.
|
protected boolean |
updateMailboxStatus(MailboxStatus ms,
java.lang.String id,
IMAPResponse response) |
public static final java.util.logging.Level IMAP_TRACE
protected static final java.lang.String TAG_PREFIX
protected static final java.lang.String US_ASCII
protected static final int DEFAULT_PORT
protected static final int DEFAULT_SSL_PORT
public static final java.util.logging.Logger logger
protected java.net.Socket socket
protected IMAPResponseTokenizer in
protected CRLFOutputStream out
protected java.util.List asyncResponses
public IMAPConnection(java.lang.String host) throws java.net.UnknownHostException, java.io.IOException
host
- the name of the host to connect tojava.net.UnknownHostException
java.io.IOException
public IMAPConnection(java.lang.String host, int port) throws java.net.UnknownHostException, java.io.IOException
host
- the name of the host to connect toport
- the port to connect to, or -1 for the defaultjava.net.UnknownHostException
java.io.IOException
public IMAPConnection(java.lang.String host, int port, int connectionTimeout, int timeout) throws java.net.UnknownHostException, java.io.IOException
host
- the name of the host to connect toport
- the port to connect to, or -1 for the defaultconnectionTimeout
- the socket connection timeouttimeout
- the socket timeoutjava.net.UnknownHostException
java.io.IOException
public IMAPConnection(java.lang.String host, int port, javax.net.ssl.TrustManager tm) throws java.net.UnknownHostException, java.io.IOException
host
- the name of the host to connect toport
- the port to connect to, or -1 for the defaulttm
- a trust manager used to check SSL certificates, or null to
use the defaultjava.net.UnknownHostException
java.io.IOException
public IMAPConnection(java.lang.String host, int port, int connectionTimeout, int timeout, boolean secure, javax.net.ssl.TrustManager tm) throws java.net.UnknownHostException, java.io.IOException
host
- the name of the host to connect toport
- the port to connect to, or -1 for the defaultconnectionTimeout
- the socket connection timeouttimeout
- the socket timeoutsecure
- if an IMAP-SSL connection should be madetm
- a trust manager used to check SSL certificates, or null to
use the defaultjava.net.UnknownHostException
java.io.IOException
public void setAnsiDebug(boolean flag)
protected java.lang.String newTag()
protected void sendCommand(java.lang.String tag, java.lang.String command) throws java.io.IOException
java.io.IOException
public boolean invokeSimpleCommand(java.lang.String command) throws java.io.IOException
command
- the commandjava.io.IOException
- if BAD was received or an I/O error occurredprotected IMAPResponse readResponse() throws java.io.IOException
java.io.IOException
public boolean alertsPending()
public java.lang.String[] getAlerts()
public java.util.List capability() throws java.io.IOException
java.io.IOException
public MailboxStatus noop() throws java.io.IOException
java.io.IOException
protected javax.net.ssl.SSLSocketFactory getSSLSocketFactory(javax.net.ssl.TrustManager tm) throws java.security.GeneralSecurityException
tm
- an optional trust manager to usejava.security.GeneralSecurityException
public boolean starttls() throws java.io.IOException
java.io.IOException
public boolean starttls(javax.net.ssl.TrustManager tm) throws java.io.IOException
tm
- the custom trust manager to usejava.io.IOException
public boolean login(java.lang.String username, java.lang.String password) throws java.io.IOException
username
- the authentication principalpassword
- the authentication credentialsjava.io.IOException
public boolean authenticate(java.lang.String mechanism, java.lang.String username, java.lang.String password) throws java.io.IOException
mechanism
- a SASL authentication mechanism, e.g. LOGIN, PLAIN,
CRAM-MD5, GSSAPIusername
- the authentication principalpassword
- the authentication credentialsjava.io.IOException
public void logout() throws java.io.IOException
java.io.IOException
public MailboxStatus select(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public MailboxStatus examine(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
protected MailboxStatus selectImpl(java.lang.String mailbox, java.lang.String command) throws java.io.IOException
java.io.IOException
protected boolean updateMailboxStatus(MailboxStatus ms, java.lang.String id, IMAPResponse response) throws java.io.IOException
java.io.IOException
public boolean create(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public boolean delete(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public boolean rename(java.lang.String source, java.lang.String target) throws java.io.IOException
source
- the source mailbox nametarget
- the target mailbox namejava.io.IOException
public boolean subscribe(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public boolean unsubscribe(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public ListEntry[] list(java.lang.String reference, java.lang.String mailbox) throws java.io.IOException
reference
- the context relative to which mailbox names are
definedmailbox
- a mailbox name, possibly including IMAP wildcardsjava.io.IOException
public ListEntry[] lsub(java.lang.String reference, java.lang.String mailbox) throws java.io.IOException
java.io.IOException
list(java.lang.String, java.lang.String)
protected ListEntry[] listImpl(java.lang.String command, java.lang.String reference, java.lang.String mailbox) throws java.io.IOException
java.io.IOException
public MailboxStatus status(java.lang.String mailbox, java.lang.String[] statusNames) throws java.io.IOException
java.io.IOException
public boolean append(java.lang.String mailbox, java.lang.String[] flags, byte[] content) throws java.io.IOException
mailbox
- the mailbox nameflags
- optional list of flags to specify for the messagecontent
- the message body(including headers)java.io.IOException
public boolean append(java.lang.String mailbox, java.lang.String[] flags, byte[] content, UIDPlusHandler uidplus) throws java.io.IOException
mailbox
- the mailbox nameflags
- optional list of flags to specify for the messagecontent
- the message body(including headers)uidplus
- handler for any APPENDUID information in the responsejava.io.IOException
public void check() throws java.io.IOException
java.io.IOException
public boolean close() throws java.io.IOException
java.io.IOException
public int[] expunge() throws java.io.IOException
java.io.IOException
public int[] search(java.lang.String charset, java.lang.String[] criteria) throws java.io.IOException
java.io.IOException
public MessageStatus fetch(int message, java.lang.String[] fetchCommands) throws java.io.IOException
message
- the message numberfetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus[] fetch(int start, int end, java.lang.String[] fetchCommands) throws java.io.IOException
start
- the message number of the first messageend
- the message number of the last messagefetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus[] fetch(int[] messages, java.lang.String[] fetchCommands) throws java.io.IOException
messages
- the message numbersfetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus uidFetch(long uid, java.lang.String[] fetchCommands) throws java.io.IOException
uid
- the message UIDfetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus[] uidFetch(long start, long end, java.lang.String[] fetchCommands) throws java.io.IOException
start
- the message number of the first messageend
- the message number of the last messagefetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus[] uidFetch(long[] uids, java.lang.String[] fetchCommands) throws java.io.IOException
uids
- the message UIDsfetchCommands
- the fetch commands, e.g. FLAGSjava.io.IOException
public MessageStatus store(int message, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
message
- the message numberflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public MessageStatus[] store(int start, int end, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
start
- the message number of the first messageend
- the message number of the last messageflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public MessageStatus[] store(int[] messages, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
messages
- the message numbersflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public MessageStatus uidStore(long uid, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
uid
- the message UIDflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public MessageStatus[] uidStore(long start, long end, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
start
- the UID of the first messageend
- the UID of the last messageflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public MessageStatus[] uidStore(long[] uids, java.lang.String flagCommand, java.lang.String[] flags) throws java.io.IOException
uids
- the message UIDsflagCommand
- FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags
- message flags to setjava.io.IOException
public boolean copy(int[] messages, java.lang.String mailbox) throws java.io.IOException
messages
- the message numbersmailbox
- the destination mailboxjava.io.IOException
public boolean copy(int[] messages, java.lang.String mailbox, UIDPlusHandler uidplus) throws java.io.IOException
messages
- the message numbersmailbox
- the destination mailboxuidplus
- UIDPLUS callback for COPYUID informationjava.io.IOException
public Namespaces namespace() throws java.io.IOException
java.io.IOException
public boolean setacl(java.lang.String mailbox, java.lang.String principal, int rights) throws java.io.IOException
mailbox
- the mailbox nameprincipal
- the authentication identifierrights
- the rights to assignjava.io.IOException
public boolean deleteacl(java.lang.String mailbox, java.lang.String principal) throws java.io.IOException
mailbox
- the mailbox nameprincipal
- the authentication identifierjava.io.IOException
public java.util.Map getacl(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public int listrights(java.lang.String mailbox, java.lang.String principal) throws java.io.IOException
mailbox
- the mailbox nameprincipal
- the authentication identityjava.io.IOException
public int myrights(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public Quota setquota(java.lang.String quotaRoot, Quota.Resource[] resources) throws java.io.IOException
quotaRoot
- the quota rootresources
- the list of resources and associated limits to setnull
if the operation failedjava.io.IOException
public Quota getquota(java.lang.String quotaRoot) throws java.io.IOException
quotaRoot
- the quota rootjava.io.IOException
public Quota[] getquotaroot(java.lang.String mailbox) throws java.io.IOException
mailbox
- the mailbox namejava.io.IOException
public int[] uidExpunge(long start, long end) throws java.io.IOException
start
- the UID of the first message to expungeend
- the UID of the last message to expungejava.io.IOException