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

236 Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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);
    }
}