Delete Emails from POP3 Server
Deleting Emails from Server
The Pop3Client class uses POP3 protocol to copy the mail messages from mailbox to your PC. Once the mail has been retrieved you do not need to be connected to the internet while it is being read as you could read the retrieved mail on PC. If you don’t need or want a copy of some mail messages kept on the POP3 server, you then delete it. This section shows how to delete emails using Pop3Client class.
Delete Email by Index
The following code snippet deletes all the mail messages of a mailbox one by one, based on its index. Index should never be <=0 in Pop3Client.DeleteMessage.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// Create a POP3 client | |
Pop3Client client = new Pop3Client("mail.aspose.com", 110, "username", "psw"); | |
try | |
{ | |
// Delete all the message one by one | |
int messageCount = client.GetMessageCount(); | |
for (int i = 1; i <= messageCount; i++) | |
{ | |
client.DeleteMessage(i); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} |
Delete All Emails
We might also call Pop3Client.DeleteMessages to delete all the messages. The following code snippet shows you how to delete all emails.
// Delete all the messages
client.DeleteMessages();
If the connection to the POP3 server is broken immediately after deleting operations, you can no longer call Cancel Deletes to do the things you want.
Cancel Email Deletions
Pop3Client.UndeleteMessages can be used to cancel the deletion of email messages. The following code snippet shows you how to cancel deletes.
// Cancel deletes
client.UndeleteMessages();