Receive and Save Email in C#, VB.NET

Spire.Email allows receiving email messages with POP3 client and IMAP client. The following examples demonstrate how to retrieve an email using both POP3 and IMAP clients and save it to disk in C# and VB.NET.

Use POP3 client

[C#]
//Create a POP3 client
Pop3Client pop = new Pop3Client();
//Set host, username, password etc. for the client
pop.Host = "outlook.office365.com";
pop.Username = "LeonDavisLD@outlook.com";
pop.Password = "password";
pop.Port = 995;
pop.EnableSsl = true;
//Connect the server
pop.Connect();

//Get the first message by its sequence number
MailMessage message = pop.GetMessage(1);

//Parse the message
Console.WriteLine("------------------ HEADERS ---------------");
Console.WriteLine("From   : " + message.From.ToString());
Console.WriteLine("To     : " + message.To.ToString());
Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
Console.WriteLine("Subject: " + message.Subject);
Console.WriteLine("------------------- BODY -----------------");
Console.WriteLine(message.BodyText);
Console.WriteLine("------------------- END ------------------");

//Save the message to disk using its subject as file name
message.Save(message.Subject + ".eml", MailMessageFormat.Eml);

Console.WriteLine("Message Saved.");
Console.ReadKey();
[VB.NET]
'Create a POP3 client
Dim pop As New Pop3Client()
'Set host, username, password etc. for the client
pop.Host = "outlook.office365.com"
pop.Username = "LeonDavisLD@outlook.com"
pop.Password = "password"
pop.Port = 995
pop.EnableSsl = True
'Connect the server
pop.Connect()

'Get the first message by its sequence number
Dim message As MailMessage = pop.GetMessage(1)

'Parse the message
Console.WriteLine("------------------ HEADERS ---------------")
Console.WriteLine("From   : " + message.From.ToString())
Console.WriteLine("To     : " + message.[To].ToString())
Console.WriteLine("Date   : " + message.[Date].ToString(CultureInfo.InvariantCulture))
Console.WriteLine("Subject: " + message.Subject)
Console.WriteLine("------------------- BODY -----------------")
Console.WriteLine(message.BodyText)
Console.WriteLine("------------------- END ------------------")

'Save the message to disk using its subject as file name
message.Save(message.Subject + ".eml", MailMessageFormat.Eml)

Console.WriteLine("Message Saved.")
Console.ReadKey()

Use IMAP client

[C#]
//Create an IMAP client
ImapClient imap = new ImapClient();
// Set host, username, password etc. for the client
imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;
//Connect the server
imap.Connect();

//Select Inbox folder
imap.Select("Inbox");

//Get the first message by its sequence number
MailMessage message = imap.GetFullMessage(1);

//Parse the message
Console.WriteLine("------------------ HEADERS ---------------");
Console.WriteLine("From   : " + message.From.ToString());
Console.WriteLine("To     : " + message.To.ToString());
Console.WriteLine("Date   : " + message.Date.ToString(CultureInfo.InvariantCulture));
Console.WriteLine("Subject: " + message.Subject);
Console.WriteLine("------------------- BODY -----------------");
Console.WriteLine(message.BodyText);
Console.WriteLine("------------------- END ------------------");

//Save the message to disk using its subject as file name
message.Save(message.Subject + ".eml", MailMessageFormat.Eml);

Console.WriteLine("Message Saved.");
Console.ReadKey();
[VB.NET]
'Create an IMAP client
Dim imap As New ImapClient()
' Set host, username, password etc. for the client
imap.Host = "outlook.office365.com"
imap.Port = 143
imap.Username = "LeonDavisLD@outlook.com"
imap.Password = "password"
imap.ConnectionProtocols = ConnectionProtocols.Ssl
'Connect the server
imap.Connect()

'Select Inbox folder
imap.[Select]("Inbox")

'Get the first message by its sequence number
Dim message As MailMessage = imap.GetFullMessage(1)

'Parse the message
Console.WriteLine("------------------ HEADERS ---------------")
Console.WriteLine("From   : " + message.From.ToString())
Console.WriteLine("To     : " + message.[To].ToString())
Console.WriteLine("Date   : " + message.[Date].ToString(CultureInfo.InvariantCulture))
Console.WriteLine("Subject: " + message.Subject)
Console.WriteLine("------------------- BODY -----------------")
Console.WriteLine(message.BodyText)
Console.WriteLine("------------------- END ------------------")

'Save the message to disk using its subject as file name
message.Save(message.Subject + ".eml", MailMessageFormat.Eml)

Console.WriteLine("Message Saved.")
Console.ReadKey()

Screenshot:

Receive and Save Email in C#, VB.NET