Delete Email messages in C#, VB.NET

This article demonstrates how to delete a specific email message along with all email messages using Spire.Email component.

Detail steps:

Step 1: Create a POP3 client.

Pop3Client pop3 = new Pop3Client();

Step 2: Set host, authentication, port and connection protocol.

pop3.Host = "outlook.office365.com";
pop3.Username = "LeonDavisLD@outlook.com";
pop3.Password = "password";
pop3.Port = 995;
pop3.EnableSsl = true;

Step 3: Connect the pop server.

pop3.Connect();

Step 4: Get the number of messages before deleting message(s).

//Get the number of messages before deleting message(s)
Pop3MessageInfoCollection messages = pop3.GetAllMessages();
Console.WriteLine("Number of messages before deleting: " + messages.Count);

Step 5: Delete message(s).

//Delete an email message by its sequence number
pop3.DeleteMessage(2);

//Delete all messages
//pop3.DeleteAllMessages();

Step 6: Get the number of messages after deleting message(s).

//Get the number of messages after deleting message(s)
messages = pop3.GetAllMessages();
Console.WriteLine("Number of messages after deleting: " + messages.Count);

Delete Email messages in C#, VB.NET

Full code:

[C#]
using Spire.Email;
using Spire.Email.Pop3;
using System;

namespace DeleteEmailMessages 
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a POP3 client
            Pop3Client pop3 = new Pop3Client();

            //Set host, authentication, port and connection protocol
            pop3.Host = "outlook.office365.com";
            pop3.Username = "LeonDavisLD@outlook.com";
            pop3.Password = "password";
            pop3.Port = 995;
            pop3.EnableSsl = true;

            //Connect the pop server
            pop3.Connect();

            //Get the number of messages before deleting message(s)
            Pop3MessageInfoCollection messages = pop3.GetAllMessages();
            Console.WriteLine("Number of messages before deleting: " + messages.Count);

            //Delete an email message by its sequence number
            pop3.DeleteMessage(2);

            //Delete all messages
            //pop3.DeleteAllMessages();

            //Get the number of messages after deleting message(s)
            messages = pop3.GetAllMessages();
            Console.WriteLine("Number of messages after deleting: " + messages.Count);       
        }
    }
}
[VB.NET]
Imports Spire.Email
Imports Spire.Email.Pop3

Namespace DeleteEmailMessages
	Class Program
		Private Shared Sub Main(args As String())
			'Create a POP3 client
			Dim pop3 As New Pop3Client()

			'Set host, authentication, port and connection protocol
			pop3.Host = "outlook.office365.com"
			pop3.Username = "LeonDavisLD@outlook.com"
			pop3.Password = "password"
			pop3.Port = 995
			pop3.EnableSsl = True

			'Connect the pop server
			pop3.Connect()

			'Get the number of messages before deleting message(s)
			Dim messages As Pop3MessageInfoCollection = pop3.GetAllMessages()
			Console.WriteLine("Number of messages before deleting: " + messages.Count)

			'Delete an email message by its sequence number
			pop3.DeleteMessage(2)

			'Delete all messages
			'pop3.DeleteAllMessages();

			'Get the number of messages after deleting message(s)
			messages = pop3.GetAllMessages()
			Console.WriteLine("Number of messages after deleting: " + messages.Count)
		End Sub
	End Class
End Namespace