Class POP3Client
- Direct Known Subclasses:
POP3SClient
Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a
MalformedServerReplyException
, which is a subclass of IOException. A MalformedServerReplyException will be thrown when the
reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as
lenient as possible.
- See Also:
-
Field Summary
Fields inherited from class org.apache.commons.net.pop3.POP3
_commandSupport_, AUTHORIZATION_STATE, DEFAULT_PORT, DISCONNECTED_STATE, TRANSACTION_STATE, UPDATE_STATE
Fields inherited from class org.apache.commons.net.SocketClient
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL, remoteInetSocketAddress
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
capa()
Send a CAPA command to the POP3 server.boolean
deleteMessage
(int messageId) Delete a message from the POP3 server.listMessage
(int messageId) List an individual message.List all messages.listUniqueIdentifier
(int messageId) List the unique identifier for a message.List the unique identifiers for all messages.boolean
Login to the POP3 server with the given user and password.boolean
Login to the POP3 server with the given username and authentication information.boolean
logout()
Logout of the POP3 server.boolean
noop()
Send a NOOP command to the POP3 server.boolean
reset()
Reset the POP3 session.retrieveMessage
(int messageId) Retrieve a message from the POP3 server.retrieveMessageTop
(int messageId, int numLines) Retrieve only the specified top number of lines of a message from the POP3 server.status()
Gets the mailbox status.Methods inherited from class org.apache.commons.net.pop3.POP3
_connectAction_, disconnect, getAdditionalReply, getCommandSupport, getReplyString, getReplyStrings, getState, removeProtocolCommandistener, sendCommand, sendCommand, sendCommand, sendCommand, setState
Methods inherited from class org.apache.commons.net.SocketClient
addProtocolCommandListener, applySocketAttributes, checkOpenOutputStream, connect, connect, connect, connect, connect, connect, createCommandSupport, fireCommandSent, fireReplyReceived, getCharset, getCharsetName, getConnectTimeout, getDefaultPort, getDefaultTimeout, getKeepAlive, getLocalAddress, getLocalPort, getProxy, getReceiveBufferSize, getRemoteAddress, getRemoteInetSocketAddress, getRemotePort, getSendBufferSize, getServerSocketFactory, getSoLinger, getSoTimeout, getTcpNoDelay, isAvailable, isConnected, removeProtocolCommandListener, setCharset, setConnectTimeout, setDefaultPort, setDefaultTimeout, setKeepAlive, setProxy, setReceiveBufferSize, setSendBufferSize, setServerSocketFactory, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote
-
Constructor Details
-
POP3Client
public POP3Client()
-
-
Method Details
-
capa
Send a CAPA command to the POP3 server.- Returns:
- True if the command was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the CAPA command.- Since:
- 3.1 (was previously in ExtendedPOP3Client)
-
deleteMessage
Delete a message from the POP3 server. The message is only marked for deletion by the server. If you decide to unmark the message, you must issue areset
command. Messages marked for deletion are only deleted by the server onlogout
. A deletion attempt can only succeed if the client is in theTRANSACTION_STATE
.- Parameters:
messageId
- The message number to delete.- Returns:
- True if the deletion attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the delete command.
-
listMessage
List an individual message. A list attempt can only succeed if the client is in theTRANSACTION_STATE
. Returns a POP3MessageInfo instance containing the number of the listed message and the size of the message in bytes. Returns null if the list attempt fails (e.g., if the specified message number does not exist).- Parameters:
messageId
- The number of the message list.- Returns:
- A POP3MessageInfo instance containing the number of the listed message and the size of the message in bytes. Returns null if the list attempt fails.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the list command.
-
listMessages
List all messages. A list attempt can only succeed if the client is in theTRANSACTION_STATE
. Returns an array of POP3MessageInfo instances, each containing the number of a message and its size in bytes. If there are no messages, this method returns a zero length array. If the list attempt fails, it returns null.- Returns:
- An array of POP3MessageInfo instances representing all messages in the order they appear in the mailbox, each containing the number of a message and its size in bytes. If there are no messages, this method returns a zero length array. If the list attempt fails, it returns null.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the list command.
-
listUniqueIdentifier
List the unique identifier for a message. A list attempt can only succeed if the client is in theTRANSACTION_STATE
. Returns a POP3MessageInfo instance containing the number of the listed message and the unique identifier for that message. Returns null if the list attempt fails (e.g., if the specified message number does not exist).- Parameters:
messageId
- The number of the message list.- Returns:
- A POP3MessageInfo instance containing the number of the listed message and the unique identifier for that message. Returns null if the list attempt fails.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the list unique identifier command.
-
listUniqueIdentifiers
List the unique identifiers for all messages. A list attempt can only succeed if the client is in theTRANSACTION_STATE
. Returns an array of POP3MessageInfo instances, each containing the number of a message and its unique identifier. If there are no messages, this method returns a zero length array. If the list attempt fails, it returns null.- Returns:
- An array of POP3MessageInfo instances representing all messages in the order they appear in the mailbox, each containing the number of a message and its unique identifier If there are no messages, this method returns a zero length array. If the list attempt fails, it returns null.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the list unique identifier command.
-
login
Login to the POP3 server with the given user and password. You must first connect to the server withconnect
before attempting to log in. A login attempt is only valid if the client is in theAUTHORIZATION_STATE
. After logging in, the client enters theTRANSACTION_STATE
.- Parameters:
user
- The account name being logged in to.password
- The plain text password of the account.- Returns:
- True if the login attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of logging in.
-
login
public boolean login(String user, String timestamp, String secret) throws IOException, NoSuchAlgorithmException Login to the POP3 server with the given username and authentication information. Use this method when connecting to a server requiring authentication using the APOP command. Because the timestamp produced in the greeting banner varies from server to server, it is not possible to consistently extract the information. Therefore, after connecting to the server, you must callgetReplyString
and parse out the timestamp information yourself.You must first connect to the server with
connect
before attempting to log in. A login attempt is only valid if the client is in theAUTHORIZATION_STATE
. After logging in, the client enters theTRANSACTION_STATE
. After connecting, you must parse out the server specific information to use as a timestamp, and pass that information to this method. The secret is a shared secret known to you and the server. See RFC 1939 for more details regarding the APOP command.- Parameters:
user
- The account name being logged in to.timestamp
- The timestamp string to combine with the secret.secret
- The shared secret which produces the MD5 digest when combined with the timestamp.- Returns:
- True if the login attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of logging in.NoSuchAlgorithmException
- If the MD5 encryption algorithm cannot be instantiated by the Java runtime system.
-
logout
Logout of the POP3 server. To fully disconnect from the server you must calldisconnect
. A logout attempt is valid in any state. If the client is in theTRANSACTION_STATE
, it enters theUPDATE_STATE
on a successful logout.- Returns:
- True if the logout attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of logging out.
-
noop
Send a NOOP command to the POP3 server. This is useful for keeping a connection alive since most POP3 servers will time out after 10 minutes of inactivity. A noop attempt will only succeed if the client is in theTRANSACTION_STATE
.- Returns:
- True if the noop attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the NOOP command.
-
reset
Reset the POP3 session. This is useful for undoing any message deletions that may have been performed. A reset attempt can only succeed if the client is in theTRANSACTION_STATE
.- Returns:
- True if the reset attempt was successful, false if not.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the reset command.
-
retrieveMessage
Retrieve a message from the POP3 server. A retrieve message attempt can only succeed if the client is in theTRANSACTION_STATE
You must not issue any commands to the POP3 server (i.e., call any other methods) until you finish reading the message from the returned BufferedReader instance. The POP3 protocol uses the same stream for issuing commands as it does for returning results. Therefore, the returned BufferedReader actually reads directly from the POP3 connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
- Parameters:
messageId
- The number of the message to fetch.- Returns:
- A DotTerminatedMessageReader instance from which the entire message can be read. This can safely be cast to a
BufferedReader
in order to use theBufferedReader#readLine()
method. Returns null if the retrieval attempt fails (e.g., if the specified message number does not exist). - Throws:
IOException
- If a network I/O error occurs in the process of sending the retrieve message command.
-
retrieveMessageTop
Retrieve only the specified top number of lines of a message from the POP3 server. A retrieve top lines attempt can only succeed if the client is in theTRANSACTION_STATE
You must not issue any commands to the POP3 server (i.e., call any other methods) until you finish reading the message from the returned BufferedReader instance. The POP3 protocol uses the same stream for issuing commands as it does for returning results. Therefore, the returned BufferedReader actually reads directly from the POP3 connection. After the end of message has been reached, new commands can be executed and their replies read. If you do not follow these requirements, your program will not work properly.
- Parameters:
messageId
- The number of the message to fetch.numLines
- The top number of lines to fetch. This must be >= 0.- Returns:
- A DotTerminatedMessageReader instance from which the specified top number of lines of the message can be read. This can safely be cast to a
BufferedReader
in order to use theBufferedReader#readLine()
method. Returns null if the retrieval attempt fails (e.g., if the specified message number does not exist). - Throws:
IOException
- If a network I/O error occurs in the process of sending the top command.
-
status
Gets the mailbox status. A status attempt can only succeed if the client is in theTRANSACTION_STATE
. Returns a POP3MessageInfo instance containing the number of messages in the mailbox and the total size of the messages in bytes. Returns null if the status the attempt fails.- Returns:
- A POP3MessageInfo instance containing the number of messages in the mailbox and the total size of the messages in bytes. Returns null if the status the attempt fails.
- Throws:
IOException
- If a network I/O error occurs in the process of sending the status command.
-