pop3client.java

来自「apache推出的net包」· Java 代码 · 共 550 行 · 第 1/2 页

JAVA
550
字号
    /***     * 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 }     * .     * <p>     * @return True if the reset attempt was successful, false if not.     * @exception IOException If a network I/O error occurs in the process of     *      sending the reset command.     ***/    public boolean reset() throws IOException    {        if (getState() == TRANSACTION_STATE)            return (sendCommand(POP3Command.RSET) == POP3Reply.OK);        return false;    }    /***     * 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.     * <p>     * @return 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.     * @exception IOException If a network I/O error occurs in the process of     *       sending the status command.     ***/    public POP3MessageInfo status() throws IOException    {        if (getState() != TRANSACTION_STATE)            return null;        if (sendCommand(POP3Command.STAT) != POP3Reply.OK)            return null;        return __parseStatus(_lastReplyLine.substring(3));    }    /***     * 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).     * <p>     * @param messageId  The number of the message list.     * @return 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.     * @exception IOException If a network I/O error occurs in the process of     *         sending the list command.     ***/    public POP3MessageInfo listMessage(int messageId) throws IOException    {        if (getState() != TRANSACTION_STATE)            return null;        if (sendCommand(POP3Command.LIST, Integer.toString(messageId))                != POP3Reply.OK)            return null;        return __parseStatus(_lastReplyLine.substring(3));    }    /***     * 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.     * <p>     * @return 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.     * @exception IOException If a network I/O error occurs in the process of     *     sending the list command.     ***/    public POP3MessageInfo[] listMessages() throws IOException    {        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;    }    /***     * 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).     * <p>     * @param messageId  The number of the message list.     * @return A POP3MessageInfo instance containing the number of the     *         listed message and the unique identifier for that message.     *         Returns null if the list attempt fails.     * @exception IOException If a network I/O error occurs in the process of     *        sending the list unique identifier command.     ***/    public POP3MessageInfo listUniqueIdentifier(int messageId)    throws IOException    {        if (getState() != TRANSACTION_STATE)            return null;        if (sendCommand(POP3Command.UIDL, Integer.toString(messageId))                != POP3Reply.OK)            return null;        return __parseUID(_lastReplyLine.substring(3));    }    /***     * 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.     * <p>     * @return 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.     * @exception IOException If a network I/O error occurs in the process of     *     sending the list unique identifier command.     ***/    public POP3MessageInfo[] listUniqueIdentifiers() throws IOException    {        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;    }    /***     * 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).     * <p>     * 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.     * <p>     * @param messageId  The number of the message to fetch.     * @return 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).     * @exception IOException If a network I/O error occurs in the process of     *        sending the retrieve message command.     ***/    public Reader retrieveMessage(int messageId) throws IOException    {        if (getState() != TRANSACTION_STATE)            return null;        if (sendCommand(POP3Command.RETR, Integer.toString(messageId))                != POP3Reply.OK)            return null;        return new DotTerminatedMessageReader(_reader);    }    /***     * 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).     * <p>     * 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.     * <p>     * @param messageId  The number of the message to fetch.     * @param numLines  The top number of lines to fetch. This must be >= 0.     * @return  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).     * @exception IOException If a network I/O error occurs in the process of     *       sending the top command.     ***/    public Reader retrieveMessageTop(int messageId, int numLines)    throws IOException    {        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);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?