public class Imap
extends java.lang.Object
implements java.lang.AutoCloseable, java.io.Serializable
// new instance with hostname, username and password Imap imap = new Imap("imap4.myserver.com","jsmith","secret"); try { // connect to IMAP4 server imap.connect(); // get messages int messageCount = imap.getMessageCount(); for(int i = 1; i <= messageCount(); ++i) { EmailMessage em = imap.getMessage(i); } // disconnect imap.disconnect(); } catch(Exception e) { System.out.println(e); }
Modifier and Type | Class and Description |
---|---|
static class |
Imap.Authentication |
Modifier and Type | Field and Description |
---|---|
static int |
AUTH_CRAM_MD5
CRAM_MD5 authentication.
|
static int |
AUTH_LOGIN
Standard username/password authentication.
|
static int |
AUTH_XOAUTH2 |
static int |
FLAG_ANSWERED
Flag used in marking a message as Answered.
|
static int |
FLAG_DELETED
Flag used in marking a message as Deleted.
|
static int |
FLAG_DRAFT
Flag used in marking a message as Draft.
|
static int |
FLAG_FLAGGED
Flag used in marking a message as Flagged.
|
static int |
FLAG_MODE_ADD
Flag mode used to add flags.
|
static int |
FLAG_MODE_DELETE
Flag mode used to delete flags.
|
static int |
FLAG_MODE_REPLACE
Flag mode used to replace flags.
|
static int |
FLAG_RECENT
Flag used in marking a message as Recent.
|
static int |
FLAG_SEEN
Flag used in marking a message as Seen.
|
Constructor and Description |
---|
Imap()
Constructs a new Imap instance.
|
Imap(java.lang.String hostname,
java.lang.String username,
java.lang.String password)
Constructs a new Imap instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addImapListener(ImapListener listener)
Adds Imap event listener.
|
void |
append(java.lang.String mailbox,
EmailMessage message)
Appends an email message to the specified mailbox.
|
void |
append(java.lang.String mailbox,
java.util.Enumeration messages)
Appends an email message to the specified mailbox.
|
void |
clearProxySettings()
Clears proxy server values.
|
void |
close() |
Imap |
connect()
Connects to an IMAP server.
|
void |
createMailbox(java.lang.String mailbox)
Creates a new mailbox on IMAP server.
|
void |
deleteMailbox(java.lang.String mailbox)
Deletes mailbox on IMAP server.
|
void |
deleteMessage(int index)
Removes a message from the current mailbox.
|
void |
deleteMessages(int startIndex,
int endIndex)
Removes range of messages from the current mailbox.
|
void |
disconnect()
Closes the IMAP session.
|
void |
expunge()
Deletes messages marked for deletion from current mailbox.
|
int |
getAuthMode()
Gets the authentication mode used when logging into IMAP4 server.
|
int |
getConnectTimeout()
Gets the timeout used when opening a connection.
|
java.io.PrintStream |
getDebugStream()
Gets PrintStream used in reporting debug statements.
|
java.lang.String |
getHostname()
Gets the name of the remote host.
|
java.util.Enumeration |
getMailboxes()
Deprecated.
this method uses the FIND ALL.MAILBOXES command as identifed in RFC 1730 and has been obsoleted by RFC 2060, it is recommended you use the listMailboxes command instead.
|
EmailMessage |
getMessage(int index)
Retrieves a message from the current mailbox.
|
int |
getMessageCount()
Gets the number of messages in the current mailbox.
|
MimeHeader |
getMessageHeader(int index,
java.lang.String name)
Gets named header for a message.
|
java.util.Enumeration |
getMessageHeaders(int index)
Gets message headers for a message.
|
int[] |
getMessageID(java.lang.String expression)
Gets messages matching specified SEARCH expression.
|
java.util.Enumeration |
getMessages()
Gets all available messages in current mailbox from IMAP server.
|
java.util.Enumeration |
getMessages(int[] ids)
Gets messages for specified message ID's.
|
java.util.Enumeration |
getMessages(java.lang.String expression)
Gets messages matching specified SEARCH expression.
|
java.util.Enumeration |
getMessagesWithFlags()
Gets all available messages with flags in current mailbox from IMAP server.
|
java.util.Enumeration |
getMessagesWithFlags(int[] ids)
Gets messages for specified message ID's with flags.
|
java.util.Enumeration |
getMessagesWithFlags(java.lang.String expression)
Gets messages with flags matching specified SEARCH expression.
|
ImapMessage |
getMessageWithFlags(int index)
Gets message from current mailbox along with message flags.
|
int[] |
getNewMessageID()
Gets an int[] representing the ID(s) of new messages in current mailbox from IMAP server.
|
java.util.Enumeration |
getNewMessages()
Gets all new messages in current mailbox from IMAP server.
|
java.util.Enumeration |
getNewMessagesWithFlags()
Gets all new messages in current mailbox with message flags.
|
int |
getPort()
Gets port of IMAP server.
|
int |
getReadTimeout()
Gets the timeout used when reading responses from server.
|
int |
getTimeout()
Deprecated.
Replaced by getConnectTimeout and getReadTimeout
|
int |
getUid(int msgId)
Gets unique message id for a message on IMAP server.
|
int[] |
getUid(int startMsgId,
int endMsgId)
Gets unique message id's for a range of messages on IMAP server.
|
java.lang.String |
getUsername()
Gets the username to be sent to the remote host.
|
boolean |
isConnected()
True, if there is a connection to the IMAP server.
|
boolean |
isDebug()
Gets debug level of IMAP session.
|
boolean |
isDelete()
Checks whether to delete messages automatically after retrieval.
|
java.lang.String |
issueCommand(java.lang.String command)
This method sends out a request and waits for a response.
|
java.util.Enumeration |
listMailboxes()
Gets all mailboxes for account.
|
java.util.Enumeration |
listMailboxes(java.lang.String mailbox,
java.lang.String filter)
Gets all mailboxes for account.
|
void |
removeImapListener(ImapListener listener)
Removes specified IMAP event listener.
|
void |
renameMailbox(java.lang.String mailbox,
java.lang.String newMailbox)
Renames mailbox on IMAP server.
|
void |
selectMailbox(java.lang.String mailbox)
Selects mailbox on IMAP server.
|
Imap |
setAuthentication(Imap.Authentication authentication) |
void |
setAuthMode(int mode)
Sets the authentication mode used when logging into IMAP4 server.
|
void |
setConnectTimeout(int timeout)
Sets the timeout for opening the connection.
|
void |
setDebug(boolean debug)
Sets debugging state.
|
void |
setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements.
|
void |
setDelete(boolean delete)
Defines whether to automatically mark messages for deletion after retrieval.
|
void |
setHostname(java.lang.String hostname)
Sets the name of the remote host to cennect to.
|
void |
setPassword(java.lang.String password)
Sets the password to be sent to the remote host.
|
void |
setPort(int port)
Sets port of IMAP server.
|
void |
setProxyAuthentication(java.lang.String proxyUsername,
java.lang.String proxyPassword)
Sets the username and password to use when for authentication with
proxy server.
|
void |
setProxyHost(java.lang.String proxyHostname,
int proxyPort)
Sets the proxy hostname and port for this connection.
|
void |
setProxyType(java.lang.String proxyType)
Sets the proxy type will be used for this connection.
|
void |
setReadTimeout(int timeout)
Sets the timeout for reading responses from server.
|
void |
setTimeout(int timeout)
Deprecated.
Replaced by setConnectTimeout and setReadTimeout
|
void |
setUsername(java.lang.String username)
Sets the username to be sent to the remote host.
|
void |
store(int message,
int mode,
int flag)
Sets message flags for a single message in an IMAP mailbox.
|
void |
store(int startMessage,
int endMessage,
int mode,
int flag)
Sets message flags for a range of messages in an IMAP mailbox.
|
public static final int AUTH_LOGIN
setAuthMode
method.public static final int AUTH_CRAM_MD5
setAuthMode
method.public static final int AUTH_XOAUTH2
public static final int FLAG_MODE_REPLACE
public static final int FLAG_MODE_ADD
public static final int FLAG_MODE_DELETE
public static final int FLAG_SEEN
public static final int FLAG_ANSWERED
public static final int FLAG_FLAGGED
public static final int FLAG_DELETED
public static final int FLAG_DRAFT
public static final int FLAG_RECENT
public Imap()
public Imap(java.lang.String hostname, java.lang.String username, java.lang.String password)
hostname
- the hostname of IMAP serverusername
- the username of IMAP userpassword
- the password of IMAP userpublic Imap setAuthentication(Imap.Authentication authentication)
public void setAuthMode(int mode)
mode
- authentication modepublic int getAuthMode()
public void setConnectTimeout(int timeout)
timeout
- the timeout in millisecondspublic int getConnectTimeout()
public boolean isConnected()
connect()
,
disconnect()
public void setDebug(boolean debug)
debug
- true
to turn debugging onsetDebugStream(java.io.PrintStream)
public boolean isDebug()
true
if debugging is onsetDebugStream(java.io.PrintStream)
public void setDebugStream(java.io.PrintStream debugStream)
debugStream
- the PrintStream to send debug statements toPrintStream
public java.io.PrintStream getDebugStream()
PrintStream
public void setDelete(boolean delete)
false
.delete
- true
to delete automaticallyexpunge()
public boolean isDelete()
false
.true
if should delete automaticallypublic void setHostname(java.lang.String hostname)
hostname
- the hostname of IMAP serverpublic java.lang.String getHostname()
remoteHost
to connect to.public java.util.Enumeration getMailboxes() throws ImapException
ImapException
- if an I/O or IMAP related error occurslistMailboxes()
public java.util.Enumeration listMailboxes() throws ImapException
ImapException
- if an I/O or IMAP related error occurspublic java.util.Enumeration listMailboxes(java.lang.String mailbox, java.lang.String filter) throws ImapException
mailbox
- the root mailbox to search under or null to return all mailboxesfilter
- the filter to use or null to return all mailboxesImapException
- if an I/O or IMAP related error occurspublic EmailMessage getMessage(int index) throws ImapException
getMessageCount()
.index
- the index of the message to retrieveEmailMessage
ImapException
- if an I/O or IMAP related error occursgetMessageCount()
,
EmailMessage
public int getMessageCount() throws ImapException
ImapException
- if an I/O or IMAP related error occurspublic MimeHeader getMessageHeader(int index, java.lang.String name) throws ImapException
index
- the index of the message to retrievename
- the name of the header to retrieveImapException
- If a Imap error occurs.MimeHeader
public java.util.Enumeration getMessageHeaders(int index) throws ImapException
index
- the index of message to retrieveImapException
- if an I/O or IMAP related error occursEnumeration
,
MimeHeader
public int[] getMessageID(java.lang.String expression) throws ImapException
expression
- a valid SEARCH keyImapException
- if an I/O or IMAP related error occurspublic ImapMessage getMessageWithFlags(int index) throws ImapException
getMessageCount()
index
- Message indexImapException
- if an I/O or IMAP related error occursgetMessageCount()
,
ImapMessage
public java.util.Enumeration getMessages(int[] ids) throws ImapException
ids
- the message ID's to retrieveImapException
- if an I/O or IMAP related error occursEmailMessage
public java.util.Enumeration getMessages(java.lang.String expression) throws ImapException
expression
- a valid SEARCH keyImapException
- if an I/O or IMAP related error occursEmailMessage
public java.util.Enumeration getMessages() throws ImapException
getMessage(int index)
method.EmailMessage
ImapException
- if I/O or IMAP related error occursEmailMessage
public java.util.Enumeration getMessagesWithFlags(int[] ids) throws ImapException
ids
- the message ID's to retrieveImapException
- if an I/O or IMAP related error occursImapMessage
public java.util.Enumeration getMessagesWithFlags(java.lang.String expression) throws ImapException
expression
- a valid SEARCH keyImapException
- if an I/O or IMAP related error occursImapMessage
public java.util.Enumeration getMessagesWithFlags() throws ImapException
getMessageWithFlags(int index)
method.ImapMessage
ImapException
- if an I/O or IMAP related error occursImapMessage
public int[] getNewMessageID() throws ImapException
ImapException
- if I/O or IMAP related error occursgetMessage(int)
,
getNewMessages()
public java.util.Enumeration getNewMessages() throws ImapException
ImapException
- if I/O or IMAP related error occursEmailMessage
,
getNewMessageID()
public java.util.Enumeration getNewMessagesWithFlags() throws ImapException
ImapException
- if an I/O or IMAP related error occursImapMessage
public void setPassword(java.lang.String password)
password
- to be sent to the remote host.public void setPort(int port)
port
- the port of IMAP serverpublic int getPort()
public void setReadTimeout(int timeout)
timeout
- the timeout in millisecondspublic int getReadTimeout()
public void setTimeout(int timeout)
timeout
- the timeout in millisecondssetConnectTimeout(int)
,
setReadTimeout(int)
public int getTimeout()
getConnectTimeout()
,
getReadTimeout()
public int getUid(int msgId) throws ImapException
msgId
- the message number to get the unique message id for. Message number is always (1)
based, first message number is 1, second is 2 and so on.ImapException
- if an I/O or IMAP related error occurspublic int[] getUid(int startMsgId, int endMsgId) throws ImapException
startMsgId
- the starting message number to get the unique message id for. Message number is always (1)
based, first message number is 1, second is 2 and so on.endMsgId
- the ending message number to get the unique message id forImapException
- if an I/O or IMAP related error occurspublic void setUsername(java.lang.String username)
username
- to be sent to the remote host.public java.lang.String getUsername()
username
to be sent.public void addImapListener(ImapListener listener)
listener
- the listener to addImapListener
public void setProxyAuthentication(java.lang.String proxyUsername, java.lang.String proxyPassword)
#clearProxySettings
method.proxyUsername
- the proxy usernameproxyPassword
- the proxy passwordclearProxySettings()
public void setProxyHost(java.lang.String proxyHostname, int proxyPort)
#clearProxySettings
method.proxyHostname
- the hostname or ip address of the proxy serverproxyPort
- the port of the proxy serverclearProxySettings()
public void setProxyType(java.lang.String proxyType)
proxyType
- The proxy type. Valid values: HTTP, SOCKS5public void clearProxySettings()
public void append(java.lang.String mailbox, java.util.Enumeration messages) throws ImapException
mailbox
- the mailbox to append tomessages
- the email message to appendImapException
- if an I/O or IMAP related error occurspublic void append(java.lang.String mailbox, EmailMessage message) throws ImapException
mailbox
- the mailbox to append tomessage
- the email message to appendImapException
- if an I/O or IMAP related error occurspublic Imap connect() throws ImapException
ImapException
- if an I/O or IMAP related error occurspublic void createMailbox(java.lang.String mailbox) throws ImapException
mailbox
- the name of the mailboxImapException
- if an I/O or IMAP related error occurspublic void deleteMailbox(java.lang.String mailbox) throws ImapException
mailbox
- the name of the mailbox to deleteImapException
- if an I/O or IMAP related error occurspublic void deleteMessage(int index) throws ImapException
getMessageCount()
.index
- the index of message to mark for deletionImapException
- if an I/O or IMAP related error occursgetMessageCount()
,
expunge()
public void deleteMessages(int startIndex, int endIndex) throws ImapException
getMessageCount()
.startIndex
- the start index of message range to mark for deletionendIndex
- the end index of message range to mark for deletionImapException
- if an I/O or IMAP related error occursgetMessageCount()
,
expunge()
public void disconnect() throws ImapException
ImapException
- if an I/O or IMAP related error occursconnect()
public void close() throws java.lang.Exception
close
in interface java.lang.AutoCloseable
java.lang.Exception
public void expunge() throws ImapException
ImapException
- if an I/O or IMAP related error occursdeleteMessage(int)
,
setDelete(boolean)
public java.lang.String issueCommand(java.lang.String command) throws ImapException
ImapException
is thrown to signal it.command
- to send to serverImapException
- if an IMAP related error occurspublic void removeImapListener(ImapListener listener)
listener
- the listener to removeImapListener
public void renameMailbox(java.lang.String mailbox, java.lang.String newMailbox) throws ImapException
mailbox
- the name of mailbox to be renamednewMailbox
- the new name of mailboxImapException
- if an I/O or IMAP related error occurspublic void selectMailbox(java.lang.String mailbox) throws ImapException
mailbox
- the name of the mailbox to selectImapException
- if an I/O or IMAP related error occurspublic void store(int message, int mode, int flag) throws ImapException
message
- the message idmode
- the flag modeflag
- the flagImapException
- if an I/O or IMAP related error occursFLAG_MODE_REPLACE
,
FLAG_MODE_ADD
,
FLAG_MODE_DELETE
,
FLAG_SEEN
,
FLAG_ANSWERED
,
FLAG_FLAGGED
,
FLAG_DELETED
,
FLAG_DRAFT
,
FLAG_RECENT
public void store(int startMessage, int endMessage, int mode, int flag) throws ImapException
startMessage
- the start message idendMessage
- the end message idmode
- the flag modeflag
- the flagImapException
- if an I/O or IMAP related error occursFLAG_MODE_REPLACE
,
FLAG_MODE_ADD
,
FLAG_MODE_DELETE
,
FLAG_SEEN
,
FLAG_ANSWERED
,
FLAG_FLAGGED
,
FLAG_DELETED
,
FLAG_DRAFT
,
FLAG_RECENT
Copyright © JSCAPE LLC. 1999-2021. All Rights Reserved