Sometimes you want to delete messages on the server. Here is an example showing you just that.
/// <summary>
/// Example showing:
/// - how to delete a specific message from a server
/// </summary>
/// <param name="hostname">Hostname of the server. For example: pop3.live.com</param>
/// <param name="port">Host port to connect to. Normally: 110 for plain POP3, 995 for SSL POP3</param>
/// <param name="useSsl">Whether or not to use SSL to connect to server</param>
/// <param name="username">Username of the user on the server</param>
/// <param name="password">Password of the user on the server</param>
/// <param name="messageNumber">
/// The number of the message to delete.
/// Must be in range [1, messageCount] where messageCount is the number of messages on the server.
/// </param>
public static void DeleteMessageOnServer(string hostname, int port, bool useSsl, string username, string password, int messageNumber)
{
// The client disconnects from the server when being disposed
using (Pop3Client client = new Pop3Client())
{
// Connect to the server
client.Connect(hostname, port, useSsl);
// Authenticate ourselves towards the server
client.Authenticate(username, password);
// Mark the message as deleted
// Notice that it is only MARKED as deleted
// POP3 requires you to "commit" the changes
// which is done by sending a QUIT command to the server
// You can also reset all marked messages, by sending a RSET command.
client.DeleteMessage(messageNumber);
// When a QUIT command is sent to the server, the connection between them are closed.
// When the client is disposed, the QUIT command will be sent to the server
// just as if you had called the Disconnect method yourself.
}
}
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.