public class IMAPConnection extends Object implements IMAPConstants
| Modifier and Type | Field and Description |
|---|---|
protected 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 Level |
IMAP_TRACE
The network trace level.
|
protected IMAPResponseTokenizer |
in
The tokenizer used to read IMAP responses from.
|
static Logger |
logger
The logger used for IMAP protocol traces.
|
protected CRLFOutputStream |
out
The output stream.
|
protected Socket |
socket
The socket used for communication with the server.
|
protected static String |
TAG_PREFIX
Prefix for tags.
|
protected static String |
US_ASCII
The encoding used to create strings for IMAP commands.
|
ACL, ALERT, APPEND, AUTHENTICATE, BAD, BODY, BODY_PEEK, BODYSTRUCTURE, BYE, CAPABILITY, CHECK, CLOSE, COPY, 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, UIDNEXT, UIDVALIDITY, UNSEEN, UNSUBSCRIBE| Constructor and Description |
|---|
IMAPConnection(String host)
Creates a new connection to the default IMAP port.
|
IMAPConnection(String host,
int port)
Creates a new connection.
|
IMAPConnection(String host,
int port,
int connectionTimeout,
int timeout)
Creates a new connection.
|
IMAPConnection(String host,
int port,
int connectionTimeout,
int timeout,
boolean secure,
TrustManager tm)
Creates a new connection.
|
IMAPConnection(String host,
int port,
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(String mailbox,
String[] flags,
byte[] content)
Append a message to the specified mailbox.
|
boolean |
authenticate(String mechanism,
String username,
String password)
Authenticates the connection using the specified SASL mechanism,
username, and password.
|
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,
String mailbox)
Copies the specified messages to the end of the destination mailbox.
|
boolean |
create(String mailbox)
Creates a mailbox with the specified name.
|
boolean |
delete(String mailbox)
Deletes the mailbox with the specified name.
|
boolean |
deleteacl(String mailbox,
String principal)
Removes any access rights for the given authentication principal on the
specified mailbox.
|
MailboxStatus |
examine(String mailbox)
Selects the specified mailbox.
|
int[] |
expunge()
Permanently removes all messages that have the \Delete flag set.
|
MessageStatus[] |
fetch(int[] messages,
String[] fetchCommands)
Retrieves data associated with messages in the mailbox.
|
MessageStatus[] |
fetch(int start,
int end,
String[] fetchCommands)
Retrieves data associated with the specified range of messages in
the mailbox.
|
MessageStatus |
fetch(int message,
String[] fetchCommands)
Retrieves data associated with the specified message in the mailbox.
|
Map |
getacl(String mailbox)
Returns the access control list for the specified mailbox.
|
String[] |
getAlerts()
Returns the pending alerts for the user-agent as an array.
|
Quota |
getquota(String quotaRoot)
Returns the specified quota root's resource usage and limits.
|
Quota[] |
getquotaroot(String mailbox)
Returns the quotas for the given mailbox.
|
protected SSLSocketFactory |
getSSLSocketFactory(TrustManager tm)
Returns a configured SSLSocketFactory to use in creating new SSL
sockets.
|
protected boolean |
invokeSimpleCommand(String command)
Sends the specified IMAP command.
|
ListEntry[] |
list(String reference,
String mailbox)
Returns a subset of names from the compete set of names available to
the client.
|
protected ListEntry[] |
listImpl(String command,
String reference,
String mailbox) |
int |
listrights(String mailbox,
String principal)
Returns the rights for the given principal for the specified mailbox.
|
boolean |
login(String username,
String password)
Login to the connection using the username and password method.
|
void |
logout()
Logout this connection.
|
ListEntry[] |
lsub(String reference,
String mailbox)
Returns a subset of subscribed names.
|
int |
myrights(String mailbox)
Returns the rights for the current principal for the specified mailbox.
|
Namespaces |
namespace()
Returns the namespaces available on the server.
|
protected 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(String source,
String target)
Renames the source mailbox to the specified name.
|
int[] |
search(String charset,
String[] criteria)
Searches the currently selected mailbox for messages matching the
specified criteria.
|
MailboxStatus |
select(String mailbox)
Selects the specified mailbox.
|
protected MailboxStatus |
selectImpl(String mailbox,
String command) |
protected void |
sendCommand(String tag,
String command)
Sends the specified IMAP tagged command to the server.
|
boolean |
setacl(String mailbox,
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(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(TrustManager tm)
Attempts to start TLS on the specified connection.
|
MailboxStatus |
status(String mailbox,
String[] statusNames)
Requests the status of the specified mailbox.
|
MessageStatus[] |
store(int[] messages,
String flagCommand,
String[] flags)
Alters data associated with messages in the mailbox.
|
MessageStatus[] |
store(int start,
int end,
String flagCommand,
String[] flags)
Alters data associated with the specified range of messages in the
mailbox.
|
MessageStatus |
store(int message,
String flagCommand,
String[] flags)
Alters data associated with the specified message in the mailbox.
|
boolean |
subscribe(String mailbox)
Adds the specified mailbox to the set of subscribed mailboxes as
returned by the LSUB command.
|
MessageStatus[] |
uidFetch(long[] uids,
String[] fetchCommands)
Retrieves data associated with messages in the mailbox.
|
MessageStatus[] |
uidFetch(long start,
long end,
String[] fetchCommands)
Retrieves data associated with the specified range of messages in
the mailbox.
|
MessageStatus |
uidFetch(long uid,
String[] fetchCommands)
Retrieves data associated with the specified message in the mailbox.
|
MessageStatus[] |
uidStore(long[] uids,
String flagCommand,
String[] flags)
Alters data associated with messages in the mailbox.
|
MessageStatus[] |
uidStore(long start,
long end,
String flagCommand,
String[] flags)
Alters data associated with the specified range of messages in the
mailbox.
|
MessageStatus |
uidStore(long uid,
String flagCommand,
String[] flags)
Alters data associated with the specified message in the mailbox.
|
boolean |
unsubscribe(String mailbox)
Removes the specified mailbox from the set of subscribed mailboxes as
returned by the LSUB command.
|
protected boolean |
updateMailboxStatus(MailboxStatus ms,
String id,
IMAPResponse response) |
public static final Logger logger
public static final Level IMAP_TRACE
protected static final String TAG_PREFIX
protected static final String US_ASCII
protected static final int DEFAULT_PORT
protected static final int DEFAULT_SSL_PORT
protected Socket socket
protected IMAPResponseTokenizer in
protected CRLFOutputStream out
protected List asyncResponses
public IMAPConnection(String host) throws UnknownHostException, IOException
host - the name of the host to connect toUnknownHostExceptionIOExceptionpublic IMAPConnection(String host, int port) throws UnknownHostException, IOException
host - the name of the host to connect toport - the port to connect to, or -1 for the defaultUnknownHostExceptionIOExceptionpublic IMAPConnection(String host, int port, int connectionTimeout, int timeout) throws UnknownHostException, 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 timeoutUnknownHostExceptionIOExceptionpublic IMAPConnection(String host, int port, TrustManager tm) throws UnknownHostException, 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 defaultUnknownHostExceptionIOExceptionpublic IMAPConnection(String host, int port, int connectionTimeout, int timeout, boolean secure, TrustManager tm) throws UnknownHostException, 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 defaultUnknownHostExceptionIOExceptionpublic void setAnsiDebug(boolean flag)
protected String newTag()
protected void sendCommand(String tag, String command) throws IOException
IOExceptionprotected boolean invokeSimpleCommand(String command) throws IOException
command - the commandIOException - if BAD was received or an I/O error occurredprotected IMAPResponse readResponse() throws IOException
IOExceptionpublic boolean alertsPending()
public String[] getAlerts()
public List capability() throws IOException
IOExceptionpublic MailboxStatus noop() throws IOException
IOExceptionprotected SSLSocketFactory getSSLSocketFactory(TrustManager tm) throws GeneralSecurityException
tm - an optional trust manager to useGeneralSecurityExceptionpublic boolean starttls()
throws IOException
IOExceptionpublic boolean starttls(TrustManager tm) throws IOException
tm - the custom trust manager to useIOExceptionpublic boolean login(String username, String password) throws IOException
username - the authentication principalpassword - the authentication credentialsIOExceptionpublic boolean authenticate(String mechanism, String username, String password) throws IOException
mechanism - a SASL authentication mechanism, e.g. LOGIN, PLAIN,
CRAM-MD5, GSSAPIusername - the authentication principalpassword - the authentication credentialsIOExceptionpublic void logout()
throws IOException
IOExceptionpublic MailboxStatus select(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic MailboxStatus examine(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionprotected MailboxStatus selectImpl(String mailbox, String command) throws IOException
IOExceptionprotected boolean updateMailboxStatus(MailboxStatus ms, String id, IMAPResponse response) throws IOException
IOExceptionpublic boolean create(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic boolean delete(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic boolean rename(String source, String target) throws IOException
source - the source mailbox nametarget - the target mailbox nameIOExceptionpublic boolean subscribe(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic boolean unsubscribe(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic ListEntry[] list(String reference, String mailbox) throws IOException
reference - the context relative to which mailbox names are
definedmailbox - a mailbox name, possibly including IMAP wildcardsIOExceptionpublic ListEntry[] lsub(String reference, String mailbox) throws IOException
IOExceptionlist(java.lang.String, java.lang.String)protected ListEntry[] listImpl(String command, String reference, String mailbox) throws IOException
IOExceptionpublic MailboxStatus status(String mailbox, String[] statusNames) throws IOException
IOExceptionpublic boolean append(String mailbox, String[] flags, byte[] content) throws IOException
mailbox - the mailbox nameflags - optional list of flags to specify for the messagecontent - the message body(including headers)IOExceptionpublic void check()
throws IOException
IOExceptionpublic boolean close()
throws IOException
IOExceptionpublic int[] expunge()
throws IOException
IOExceptionpublic int[] search(String charset, String[] criteria) throws IOException
IOExceptionpublic MessageStatus fetch(int message, String[] fetchCommands) throws IOException
message - the message numberfetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus[] fetch(int start, int end, String[] fetchCommands) throws IOException
start - the message number of the first messageend - the message number of the last messagefetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus[] fetch(int[] messages, String[] fetchCommands) throws IOException
messages - the message numbersfetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus uidFetch(long uid, String[] fetchCommands) throws IOException
uid - the message UIDfetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus[] uidFetch(long start, long end, String[] fetchCommands) throws IOException
start - the message number of the first messageend - the message number of the last messagefetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus[] uidFetch(long[] uids, String[] fetchCommands) throws IOException
uids - the message UIDsfetchCommands - the fetch commands, e.g. FLAGSIOExceptionpublic MessageStatus store(int message, String flagCommand, String[] flags) throws IOException
message - the message numberflagCommand - FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags - message flags to setIOExceptionpublic MessageStatus[] store(int start, int end, String flagCommand, String[] flags) throws 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 setIOExceptionpublic MessageStatus[] store(int[] messages, String flagCommand, String[] flags) throws IOException
messages - the message numbersflagCommand - FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags - message flags to setIOExceptionpublic MessageStatus uidStore(long uid, String flagCommand, String[] flags) throws IOException
uid - the message UIDflagCommand - FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags - message flags to setIOExceptionpublic MessageStatus[] uidStore(long start, long end, String flagCommand, String[] flags) throws IOException
start - the UID of the first messageend - the UID of the last messageflagCommand - FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags - message flags to setIOExceptionpublic MessageStatus[] uidStore(long[] uids, String flagCommand, String[] flags) throws IOException
uids - the message UIDsflagCommand - FLAGS, +FLAGS, -FLAGS(or .SILENT versions)flags - message flags to setIOExceptionpublic boolean copy(int[] messages,
String mailbox)
throws IOException
messages - the message numbersmailbox - the destination mailboxIOExceptionpublic Namespaces namespace() throws IOException
IOExceptionpublic boolean setacl(String mailbox, String principal, int rights) throws IOException
mailbox - the mailbox nameprincipal - the authentication identifierrights - the rights to assignIOExceptionpublic boolean deleteacl(String mailbox, String principal) throws IOException
mailbox - the mailbox nameprincipal - the authentication identifierIOExceptionpublic Map getacl(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic int listrights(String mailbox, String principal) throws IOException
mailbox - the mailbox nameprincipal - the authentication identityIOExceptionpublic int myrights(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionpublic Quota setquota(String quotaRoot, Quota.Resource[] resources) throws IOException
quotaRoot - the quota rootresources - the list of resources and associated limits to setnull if the operation failedIOExceptionpublic Quota getquota(String quotaRoot) throws IOException
quotaRoot - the quota rootIOExceptionpublic Quota[] getquotaroot(String mailbox) throws IOException
mailbox - the mailbox nameIOExceptionCopyright © 2017. All rights reserved.