You might just have downloaded a message from the server, and later found that you want to delete the message from the server.
This requires you to find the same message on the server again, and then deleting it using the message number it has on the server.
Using the MessageID which is in the headers of the message, it is possible to find the message on the server again and delete it.
The snippit below will do just this.
/// <summary>
/// Example showing:
/// - how to delete fetch an emails headers only
/// - how to delete a message from the server
/// </summary>
/// <param name="client">A connected and authenticated Pop3Client from which to delete a message</param>
/// <param name="messageId">A message ID of a message on the POP3 server. Is located in <see cref="MessageHeader.MessageId"/></param>
/// <returns><see langword="true"/> if message was deleted, <see langword="false"/> otherwise</returns>
public bool DeleteMessageByMessageId(Pop3Client client, string messageId)
{
// Get the number of messages on the POP3 server
int messageCount = client.GetMessageCount();
// Run trough each of these messages and download the headers
for (int messageItem = messageCount; messageItem > 0; messageItem--)
{
// If the Message ID of the current message is the same as the parameter given, delete that message
if (client.GetMessageHeaders(messageItem).MessageId == messageId)
{
// Delete
client.DeleteMessage(messageItem);
return true;
}
}
// We did not find any message with the given messageId, report this back
return false;
}
Notice that it runs trough all the messages on the server and then downloads the headers for each, checking if the Message ID's agree.
This is expensive, and therefore if you have the message number of the message to delete, you should delete it directly using the DeleteMessage method of the Pop3Client.
It must be pointed out that it is important to call the Disconnect() method. If this is not done, the message will not be deleted.
The POP3 protocol specifies that emails are only marked as deleted, and a special QUIT command
must be sent to commit the changes, actually deleting the message at that point.
The Dispose() method of the Pop3Client will send this QUIT command. This is not done in the example, but will need to be done when the Pop3Client is no longer needed.