Search code examples
c#emailmessageopenpop

Delete message that do not meet the condition in OpenPOP (C#)


I am using OpenPOP library. I want to delete all messages that have a subject name different from "my_secret_subject". I wrote a function, but it removes only one message:

int messageCount = client.GetMessageCount();
if (client.GetMessageHeaders(messageCount).Subject != "my_secret_subject")
{
    client.DeleteMessage(messageCount);
}

How to write a loop that deletes all the messages that do not meet the condition? I try with "for" before if and in if. Not work.


Solution

  • Just use a for loop from 1 to messageCount:

    int messageCount = client.GetMessageCount();
    
    for(int i = 1; i <= messageCount; i++)
    {
        if (client.GetMessageHeaders(i).Subject != "my_secret_subject")
        {
            client.DeleteMessage(i);
        }
    }