FileDocCategorySizeDatePackage
POP3.javaAPI DocApache Commons NET 1.4.1 API11473Sat Dec 03 10:05:48 GMT 2005org.apache.commons.net.pop3

POP3

public class POP3 extends org.apache.commons.net.SocketClient
The POP3 class is not meant to be used by itself and is provided only so that you may easily implement your own POP3 client if you so desire. If you have no need to perform your own implementation, you should use {@link org.apache.commons.net.pop3.POP3Client}.

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 {@link org.apache.commons.net.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.

author
Daniel F. Savarese
see
POP3Client
see
org.apache.commons.net.MalformedServerReplyException

Fields Summary
public static final int
DEFAULT_PORT
The default POP3 port. Set to 110 according to RFC 1288.
public static final int
DISCONNECTED_STATE
A constant representing the state where the client is not yet connected to a POP3 server.
public static final int
AUTHORIZATION_STATE
A constant representing the POP3 authorization state.
public static final int
TRANSACTION_STATE
A constant representing the POP3 transaction state.
public static final int
UPDATE_STATE
A constant representing the POP3 update state.
static final String
_OK
static final String
_ERROR
private static final String
__DEFAULT_ENCODING
private int
__popState
private BufferedWriter
__writer
private StringBuffer
__commandBuffer
BufferedReader
_reader
int
_replyCode
String
_lastReplyLine
Vector
_replyLines
protected org.apache.commons.net.ProtocolCommandSupport
_commandSupport_
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
Constructors Summary
public POP3()
The default POP3Client constructor. Initializes the state to DISCONNECTED_STATE.


                   
     
    
        setDefaultPort(DEFAULT_PORT);
        __commandBuffer = new StringBuffer();
        __popState = DISCONNECTED_STATE;
        _reader = null;
        __writer = null;
        _replyLines = new Vector();
        _commandSupport_ = new ProtocolCommandSupport(this);
    
Methods Summary
private void__getReply()

        String line;

        _replyLines.setSize(0);
        line = _reader.readLine();

        if (line == null)
            throw new EOFException("Connection closed without indication.");

        if (line.startsWith(_OK))
            _replyCode = POP3Reply.OK;
        else if (line.startsWith(_ERROR))
            _replyCode = POP3Reply.ERROR;
        else
            throw new
            MalformedServerReplyException(
                "Received invalid POP3 protocol response from server.");

        _replyLines.addElement(line);
        _lastReplyLine = line;

        if (_commandSupport_.getListenerCount() > 0)
            _commandSupport_.fireReplyReceived(_replyCode, getReplyString());
    
protected void_connectAction_()
Performs connection initialization and sets state to AUTHORIZATION_STATE .

        super._connectAction_();
        _reader =
          new BufferedReader(new InputStreamReader(_input_,
                                                   __DEFAULT_ENCODING));
        __writer =
          new BufferedWriter(new OutputStreamWriter(_output_,
                                                    __DEFAULT_ENCODING));
        __getReply();
        setState(AUTHORIZATION_STATE);
    
public voidaddProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener listener)
Adds a ProtocolCommandListener. Delegates this task to {@link #_commandSupport_ _commandSupport_ }.

param
listener The ProtocolCommandListener to add.

        _commandSupport_.addProtocolCommandListener(listener);
    
public voiddisconnect()
Disconnects the client from the server, and sets the state to DISCONNECTED_STATE . The reply text information from the last issued command is voided to allow garbage collection of the memory used to store that information.

exception
IOException If there is an error in disconnecting.

        super.disconnect();
        _reader = null;
        __writer = null;
        _lastReplyLine = null;
        _replyLines.setSize(0);
        setState(DISCONNECTED_STATE);
    
public voidgetAdditionalReply()
Retrieves the additional lines of a multi-line server reply.

        String line;

        line = _reader.readLine();
        while (line != null)
        {
            _replyLines.addElement(line);
            if (line.equals("."))
                break;
            line = _reader.readLine();
        }
    
public java.lang.StringgetReplyString()
Returns the reply to the last command sent to the server. The value is a single string containing all the reply lines including newlines. If the reply is a single line, but its format ndicates it should be a multiline reply, then you must call {@link #getAdditionalReply getAdditionalReply() } to fetch the rest of the reply, and then call getReplyString again. You only have to worry about this if you are implementing your own client using the {@link #sendCommand sendCommand } methods.

return
The last server response.

        Enumeration en;
        StringBuffer buffer = new StringBuffer(256);

        en = _replyLines.elements();
        while (en.hasMoreElements())
        {
            buffer.append((String)en.nextElement());
            buffer.append(SocketClient.NETASCII_EOL);
        }

        return buffer.toString();
    
public java.lang.String[]getReplyStrings()
Returns an array of lines received as a reply to the last command sent to the server. The lines have end of lines truncated. If the reply is a single line, but its format ndicates it should be a multiline reply, then you must call {@link #getAdditionalReply getAdditionalReply() } to fetch the rest of the reply, and then call getReplyStrings again. You only have to worry about this if you are implementing your own client using the {@link #sendCommand sendCommand } methods.

return
The last server response.

        String[] lines;
        lines = new String[_replyLines.size()];
        _replyLines.copyInto(lines);
        return lines;
    
public intgetState()
Returns the current POP3 client state.

return
The current POP3 client state.

        return __popState;
    
public voidremoveProtocolCommandistener(org.apache.commons.net.ProtocolCommandListener listener)
Removes a ProtocolCommandListener. Delegates this task to {@link #_commandSupport_ _commandSupport_ }.

param
listener The ProtocolCommandListener to remove.

        _commandSupport_.removeProtocolCommandListener(listener);
    
public intsendCommand(java.lang.String command, java.lang.String args)
Sends a command an arguments to the server and returns the reply code.

param
command The POP3 command to send.
param
args The command arguments.
return
The server reply code (either POP3Reply.OK or POP3Reply.ERROR).

        String message;

        __commandBuffer.setLength(0);
        __commandBuffer.append(command);

        if (args != null)
        {
            __commandBuffer.append(' ");
            __commandBuffer.append(args);
        }
        __commandBuffer.append(SocketClient.NETASCII_EOL);

        __writer.write(message = __commandBuffer.toString());
        __writer.flush();

        if (_commandSupport_.getListenerCount() > 0)
            _commandSupport_.fireCommandSent(command, message);

        __getReply();
        return _replyCode;
    
public intsendCommand(java.lang.String command)
Sends a command with no arguments to the server and returns the reply code.

param
command The POP3 command to send.
return
The server reply code (either POP3Reply.OK or POP3Reply.ERROR).

        return sendCommand(command, null);
    
public intsendCommand(int command, java.lang.String args)
Sends a command an arguments to the server and returns the reply code.

param
command The POP3 command to send (one of the POP3Command constants).
param
args The command arguments.
return
The server reply code (either POP3Reply.OK or POP3Reply.ERROR).

        return sendCommand(POP3Command._commands[command], args);
    
public intsendCommand(int command)
Sends a command with no arguments to the server and returns the reply code.

param
command The POP3 command to send (one of the POP3Command constants).
return
The server reply code (either POP3Reply.OK or POP3Reply.ERROR).

        return sendCommand(POP3Command._commands[command], null);
    
public voidsetState(int state)
Sets POP3 client state. This must be one of the _STATE constants.

param
state The new state.

        __popState = state;