Receive and Save Email in C#, VB.NET

2017-07-03 08:00:34 Written by  support iceblue
Rate this item
(0 votes)

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#]
using Spire.Email;
using Spire.Email.Pop3;
using System;
using System.Globalization;

namespace ReceiveAndSaveEmailByUsingPOP3client
{
    class Program
    {
        static void Main(string[] args)
        {
            //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]
Imports Spire.Email
Imports Spire.Email.Pop3
Imports System.Globalization

Namespace ReceiveAndSaveEmailByUsingPOP3client
	Class Program
		Private Shared Sub Main(args As String())
			'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()
		End Sub
	End Class
End Namespace

Use IMAP client

[C#]
using Spire.Email;
using Spire.Email.IMap;
using System;
using System.Globalization;


namespace ReceiveAndSaveEmailByUsingIMAPclient
{
    class Program
    {
        static void Main(string[] args)
        {
            //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]
Imports Spire.Email
Imports Spire.Email.IMap
Imports System.Globalization


Namespace ReceiveAndSaveEmailByUsingIMAPclient
	Class Program
		Private Shared Sub Main(args As String())
			'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()
		End Sub
	End Class
End Namespace

Screenshot:

Receive and Save Email in C#, VB.NET

Additional Info

  • tutorial_title:
Last modified on Wednesday, 15 September 2021 03:30