Tim
Tim

Reputation: 2911

Cannot delete an email message using POP3

I have code that uses POP3 to access an email account and search for messages that were sent but the address did not exist. After processing them, I delete the failure message. I have one client who can get and process the messages, but is not able to delete them. They keep getting the message Error deleting message 1: -ERR The specified message is out of range.

The code for my delete method is below. This works for most clients, and is pretty simple, so I'm not sure why it isn't working.

    public bool Delete(int index)
    {
        bool result = false;
        String response = SendReceive("DELE ", index.ToString());
        if (response.StartsWith("+OK"))
            result = true;
        else
            logger.Log("Error deleting message " + index + ": " + response, Verbose.LogImportant);

        return result;
    }

For the SendReceive method:

    private String SendReceive(String command, String parameter)
    {
        String result = null;
        try
        {
            String myCommand = command.ToUpper().Trim() + " " + parameter.Trim() + Environment.NewLine;
            byte[] data = System.Text.Encoding.ASCII.GetBytes(myCommand.ToCharArray());
            tcpClient.GetStream().Write(data, 0, data.Length);
            result = streamReader.ReadLine();
        }
        catch { }   //  Not logged in...
        return result;
    }

The index is taken directly from the received email, and the connection is not closed until after the delete method has processed. Since there has to be an email returned to run this method, and since the index runs from 1 to n, with a 1 being sent, I don't see what is causing this to fail.

Upvotes: 2

Views: 541

Answers (1)

jstedfast
jstedfast

Reputation: 38573

The SendReceive() call looks wrong. My guess is that it should probably have a {0} in the format string. In other words, your code is probably sending DELE instead of DELE 1.

Upvotes: 1

Related Questions