News Category

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

Following code snippets demonstrate how to send an email with HTML body using Spire.Email in C# and VB.NET.

Step 1: Create an instance of MailMessage class and specify sender and recipient in its constructor.

MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);

Step 2: Set the creation date, subject and html body of the message.

message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";   
message.BodyHtml = htmlString;

Step 3: Create a SmtpClient instance, set its properties, and send the email using SendOne() medthod.

SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);

Output:

Send Email with HTML Body in C#, VB.NET

Full Code:

[C#]
MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
MailMessage message = new MailMessage(addressFrom, addressTo);

message.Date = DateTime.Now;
message.Subject = "Sending Email with HTML Body";
string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";   
message.BodyHtml = htmlString;

SmtpClient client= new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = addressFrom.Address;
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendOne(message);

Console.WriteLine("Sent Successfully!");
Console.Read();
[VB.NET]
using Spire.Email;
using Spire.Email.IMap;
using Spire.Email.Smtp;
using System;

namespace SendEmailwithHTMLBody
{
    class Program
    {
        static void Main(string[] args)
        {
            MailAddress addressFrom = new MailAddress("jack.du@e-iceblue.com", "Jack Du");
            MailAddress addressTo = new MailAddress("susanwong32@outlook.com");
            MailMessage message = new MailMessage(addressFrom, addressTo);

            message.Date = DateTime.Now;
            message.Subject = "Sending Email with HTML Body";
            string htmlString = @"<html>
                      <body>
                      <p>Dear Ms. Susan,</p>
                      <p>Thank you for your letter of yesterday inviting me to come for an interview on Friday afternoon, 5th July, at 2:30.
                              I shall be happy to be there as requested and will bring my diploma and other papers with me.</p>
                      <p>Sincerely,<br>-Jack</br></p>
                      </body>
                      </html>
                     ";
            message.BodyHtml = htmlString;

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.outlook.com";
            client.Port = 587;
            client.Username = addressFrom.Address;
            client.Password = "password";
            client.ConnectionProtocols = ConnectionProtocols.Ssl;
            client.SendOne(message);

            Console.WriteLine("Sent Successfully!");
            Console.Read();
        }
    }
}

This article illustrates how to create an email message with attachment and send it using Spire.Email component in C# and VB.NET.

Detail steps:

Step 1: Declare and assign three MailAddress objects.

MailAddress addressFrom = "Alice.yang@e-iceblue.com";
MailAddress addressTo = "leondavisld@outlook.com";
MailAddress adressCC = "Shawn_Smithhh@outlook.com";

Step 2: Create an email message.

MailMessage message = new MailMessage(addressFrom, addressTo);

Step 3: Set subject, text body and creation time for the message.

message.Subject = "Spire.Email Component";
message.BodyText = "Hi!\r\n"+
                "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform.";
message.Date = DateTime.Now;

Step 4: Add an attachment and the second receiver to cc.

message.Attachments.Add(new Attachment("Hydrangeas.jpg"));
message.Cc.Add(adressCC.Address);

Step 5: Create a SmtpClient instance and send the email message.

SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.outlook.com";
smtp.ConnectionProtocols = ConnectionProtocols.Ssl;
smtp.Username = addressFrom.Address;
smtp.Password = "password";
smtp.Port = 587;
smtp.SendOne(message);

Screenshot:

Send Email with Attachment in C#, VB.NET

Full code:

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

namespace Send_Email
{
    class Program
    {
        static void Main(string[] args)
        {
            MailAddress addressFrom = "Alice.yang@e-iceblue.com";
            MailAddress addressTo = "LeonDavisLD@outlook.com";
            MailAddress adressCC = "Shawn_Smithhh@outlook.com";

            MailMessage message = new MailMessage(addressFrom, addressTo);            

            message.Subject = "Spire.Email Component";
            message.BodyText = "Hi!\r\n"+
                "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform.";
            message.Date = DateTime.Now;

            message.Attachments.Add(new Attachment("Hydrangeas.jpg"));
            message.Cc.Add(adressCC.Address);
            
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.outlook.com";
            smtp.ConnectionProtocols = ConnectionProtocols.Ssl;
            smtp.Username = addressFrom.Address;
            smtp.Password = "password";
            smtp.Port = 587;

            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("Attachment: " + message.Attachments.Count);
            Console.WriteLine("------------------- BODY -----------------");
            Console.WriteLine(message.BodyText);            
            Console.WriteLine("------------------- END ------------------");
            smtp.SendOne(message);

            Console.WriteLine("Message Sent.");
            Console.ReadLine();    
        }
    }
}
[VB.NET]
Imports System.Globalization
Imports Spire.Email
Imports Spire.Email.IMap
Imports Spire.Email.Smtp

Namespace Send_Email
	Class Program
		Private Shared Sub Main(args As String())
			Dim addressFrom As MailAddress = "Alice.yang@e-iceblue.com"
			Dim addressTo As MailAddress = "LeonDavisLD@outlook.com"
			Dim adressCC As MailAddress = "Shawn_Smithhh@outlook.com"

			Dim message As New MailMessage(addressFrom, addressTo)

			message.Subject = "Spire.Email Component"
			message.BodyText = "Hi!" & vbCr & vbLf + "Spire.Email for .NET is a professional .NET Email library specially designed for developers to create, read and manipulate emails on any .NET (C#, VB.NET, ASP.NET) platform."
			message.[Date] = DateTime.Now

			message.Attachments.Add(New Attachment("Hydrangeas.jpg"))
			message.Cc.Add(adressCC.Address)

			Dim smtp As New SmtpClient()
			smtp.Host = "smtp.outlook.com"
			smtp.ConnectionProtocols = ConnectionProtocols.Ssl
			smtp.Username = addressFrom.Address
			smtp.Password = "password"
			smtp.Port = 587

			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("Attachment: " + message.Attachments.Count)
			Console.WriteLine("------------------- BODY -----------------")
			Console.WriteLine(message.BodyText)
			Console.WriteLine("------------------- END ------------------")
			smtp.SendOne(message)

			Console.WriteLine("Message Sent.")
			Console.ReadLine()
		End Sub
	End Class
End Namespace
Page 2 of 2