Methods Summary |
---|
private static POP3MessageInfo | __parseStatus(java.lang.String line)
int num, size;
StringTokenizer tokenizer;
tokenizer = new StringTokenizer(line);
if (!tokenizer.hasMoreElements())
return null;
num = size = 0;
try
{
num = Integer.parseInt(tokenizer.nextToken());
if (!tokenizer.hasMoreElements())
return null;
size = Integer.parseInt(tokenizer.nextToken());
}
catch (NumberFormatException e)
{
return null;
}
return new POP3MessageInfo(num, size);
|
private static POP3MessageInfo | __parseUID(java.lang.String line)
int num;
StringTokenizer tokenizer;
tokenizer = new StringTokenizer(line);
if (!tokenizer.hasMoreElements())
return null;
num = 0;
try
{
num = Integer.parseInt(tokenizer.nextToken());
if (!tokenizer.hasMoreElements())
return null;
line = tokenizer.nextToken();
}
catch (NumberFormatException e)
{
return null;
}
return new POP3MessageInfo(num, line);
|
public boolean | deleteMessage(int messageId)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 issuse a {@link #reset reset } command. Messages marked
for deletion are only deleted by the server on
{@link #logout logout }.
A delete attempt can only succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
.
if (getState() == TRANSACTION_STATE)
return (sendCommand(POP3Command.DELE, Integer.toString(messageId))
== POP3Reply.OK);
return false;
|
public POP3MessageInfo | listMessage(int messageId)List an individual message. A list attempt can only
succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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).
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.LIST, Integer.toString(messageId))
!= POP3Reply.OK)
return null;
return __parseStatus(_lastReplyLine.substring(3));
|
public POP3MessageInfo[] | listMessages()List all messages. A list attempt can only
succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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.
POP3MessageInfo[] messages;
Enumeration en;
int line;
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.LIST) != POP3Reply.OK)
return null;
getAdditionalReply();
// This could be a zero length array if no messages present
messages = new POP3MessageInfo[_replyLines.size() - 2];
en = _replyLines.elements();
// Skip first line
en.nextElement();
// Fetch lines.
for (line = 0; line < messages.length; line++)
messages[line] = __parseStatus((String)en.nextElement());
return messages;
|
public POP3MessageInfo | listUniqueIdentifier(int messageId)List the unique identifier for a message. A list attempt can only
succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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).
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.UIDL, Integer.toString(messageId))
!= POP3Reply.OK)
return null;
return __parseUID(_lastReplyLine.substring(3));
|
public POP3MessageInfo[] | listUniqueIdentifiers()List the unique identifiers for all messages. A list attempt can only
succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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.
POP3MessageInfo[] messages;
Enumeration en;
int line;
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.UIDL) != POP3Reply.OK)
return null;
getAdditionalReply();
// This could be a zero length array if no messages present
messages = new POP3MessageInfo[_replyLines.size() - 2];
en = _replyLines.elements();
// Skip first line
en.nextElement();
// Fetch lines.
for (line = 0; line < messages.length; line++)
messages[line] = __parseUID((String)en.nextElement());
return messages;
|
public boolean | login(java.lang.String username, java.lang.String password)Login to the POP3 server with the given username and password. You
must first connect to the server with
{@link org.apache.commons.net.SocketClient#connect connect }
before attempting to login. A login attempt is only valid if
the client is in the
{@link org.apache.commons.net.pop3.POP3#AUTHORIZATION_STATE AUTHORIZATION_STATE }
. After logging in, the client enters the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
.
if (getState() != AUTHORIZATION_STATE)
return false;
if (sendCommand(POP3Command.USER, username) != POP3Reply.OK)
return false;
if (sendCommand(POP3Command.PASS, password) != POP3Reply.OK)
return false;
setState(TRANSACTION_STATE);
return true;
|
public boolean | login(java.lang.String username, java.lang.String timestamp, java.lang.String secret)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 call
{@link org.apache.commons.net.pop3.POP3#getReplyString getReplyString }
and parse out the timestamp information yourself.
You must first connect to the server with
{@link org.apache.commons.net.SocketClient#connect connect }
before attempting to login. A login attempt is only valid if
the client is in the
{@link org.apache.commons.net.pop3.POP3#AUTHORIZATION_STATE AUTHORIZATION_STATE }
. After logging in, the client enters the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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.
int i;
byte[] digest;
StringBuffer buffer, digestBuffer;
MessageDigest md5;
if (getState() != AUTHORIZATION_STATE)
return false;
md5 = MessageDigest.getInstance("MD5");
timestamp += secret;
digest = md5.digest(timestamp.getBytes());
digestBuffer = new StringBuffer(128);
for (i = 0; i < digest.length; i++)
digestBuffer.append(Integer.toHexString(digest[i] & 0xff));
buffer = new StringBuffer(256);
buffer.append(username);
buffer.append(' ");
buffer.append(digestBuffer.toString());
if (sendCommand(POP3Command.APOP, buffer.toString()) != POP3Reply.OK)
return false;
setState(TRANSACTION_STATE);
return true;
|
public boolean | logout()Logout of the POP3 server. To fully disconnect from the server
you must call
{@link org.apache.commons.net.pop3.POP3#disconnect disconnect }.
A logout attempt is valid in any state. If
the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
, it enters the
{@link org.apache.commons.net.pop3.POP3#UPDATE_STATE UPDATE_STATE }
on a successful logout.
if (getState() == TRANSACTION_STATE)
setState(UPDATE_STATE);
sendCommand(POP3Command.QUIT);
return (_replyCode == POP3Reply.OK);
|
public boolean | noop()Send a NOOP command to the POP3 server. This is useful for keeping
a connection alive since most POP3 servers will timeout after 10
minutes of inactivity. A noop attempt will only succeed if
the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
.
if (getState() == TRANSACTION_STATE)
return (sendCommand(POP3Command.NOOP) == POP3Reply.OK);
return false;
|
public boolean | 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 the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
.
if (getState() == TRANSACTION_STATE)
return (sendCommand(POP3Command.RSET) == POP3Reply.OK);
return false;
|
public java.io.Reader | retrieveMessage(int messageId)Retrieve a message from the POP3 server. A retrieve message attempt
can only succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
. Returns a DotTerminatedMessageReader instance
from which the entire message can be read.
Returns null if the retrieval attempt fails (e.g., if the specified
message number does not exist).
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 Reader instance.
The POP3 protocol uses the same stream for issuing commands as it does
for returning results. Therefore the returned Reader 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.
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.RETR, Integer.toString(messageId))
!= POP3Reply.OK)
return null;
return new DotTerminatedMessageReader(_reader);
|
public java.io.Reader | retrieveMessageTop(int messageId, int numLines)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 the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_STATE }
. Returns a DotTerminatedMessageReader instance
from which the specified top number of lines of the message can be
read.
Returns null if the retrieval attempt fails (e.g., if the specified
message number does not exist).
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
Reader instance.
The POP3 protocol uses the same stream for issuing commands as it does
for returning results. Therefore the returned Reader 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.
if (numLines < 0 || getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.TOP, Integer.toString(messageId) + " " +
Integer.toString(numLines)) != POP3Reply.OK)
return null;
return new DotTerminatedMessageReader(_reader);
|
public POP3MessageInfo | status()Get the mailbox status. A status attempt can only
succeed if the client is in the
{@link org.apache.commons.net.pop3.POP3#TRANSACTION_STATE TRANSACTION_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.
if (getState() != TRANSACTION_STATE)
return null;
if (sendCommand(POP3Command.STAT) != POP3Reply.OK)
return null;
return __parseStatus(_lastReplyLine.substring(3));
|