Tuesday, 25 July 2017 07:29

Search Email Messages in C#, VB.NET

Spire.Email allows developers to search mailbox for email messages that match the given search criteria. This article illustrates how to search email messages using Spire.Email component.

Detail steps:

Step 1: Create an ImapClient instance.

ImapClient imap = new ImapClient();

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

imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;

Step 3: connect the imap server.

imap.Connect();

Step 4: Select Inbox folder.

imap.Select("Inbox");

Step 5: Search email messages in the folder that match the search criteria.

//Search email messages sent from “Alice”
ImapMessageCollection messages = imap.Search("'From' Contains 'Alice'");
Console.WriteLine("Number of messages sent from Alice: " + messages.Count);

//Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'");
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count);

Screenshot:

Search Email Messages in C#, VB.NET

Full code:

[C#]
//Create an ImapClient instance
ImapClient imap = new ImapClient();

//Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com";
imap.Port = 143;
imap.Username = "LeonDavisLD@outlook.com";
imap.Password = "password";
imap.ConnectionProtocols = ConnectionProtocols.Ssl;

//Connect the imap server
imap.Connect();

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

//Search email messages sent from "Alice"
ImapMessageCollection messages = imap.Search("'From' Contains 'Alice'");
Console.WriteLine("Number of messages sent from Alice: " + messages.Count);

//Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'");
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count);
[VB.NET]
'Create an ImapClient instance
Dim imap As New ImapClient()

'Set host, port, authentication and connection protocol
imap.Host = "outlook.office365.com"
imap.Port = 143
imap.Username = "LeonDavisLD@outlook.com"
imap.Password = "password"
imap.ConnectionProtocols = ConnectionProtocols.Ssl

'Connect the imap server
imap.Connect()

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

'Search email messages sent from "Alice"
Dim messages As ImapMessageCollection = imap.Search("'From' Contains 'Alice'")
Console.WriteLine("Number of messages sent from Alice: " + messages.Count)

'Search email messages with “Spire” string in subject
messages = imap.Search("'Subject' Contains 'Spire'")
Console.WriteLine("Number of messages with 'Spire' in subject: " + messages.Count)
Thursday, 20 July 2017 07:00

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#]
//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]
'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 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)

Spire.Doc included a variety of WordArt types in the ShapeType enum. In order to create WordArt in Word, initialize an instance of ShapeObject and specify the WordArt type and content. The following sections show you how to create WordArt using Spire.Doc step by step.

Step 1: Create an instance of Document class.

Document doc = new Document();

Step 2: Add a section, add a paragraph in section.

Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();

Step 3: Create a ShapeObject instance and specify TextWave4 as the ShapeType. Specify the content of WordArt.

ShapeObject shape= paragraph.AppendShape(120, 30, ShapeType.TextWave4);
shape.WordArt.Text = "Thanks for reading.";

Step 4: Set the position, fill color and stroke color of the shape (WordArt).

shape.VerticalPosition = 80;
shape.HorizontalPosition = 170;
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Yellow;

Step 5: Save the file.

doc.SaveToFile("output.docx", FileFormat.Docx2013);

Output:

How to Create WordArt in Word in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
Section section = doc.AddSection();
Paragraph paragraph = section.AddParagraph();

ShapeObject shape= paragraph.AppendShape(120, 30, ShapeType.TextWave4);
shape.VerticalPosition = 80;
shape.HorizontalPosition = 170;
shape.WordArt.Text = "Thanks for reading.";
shape.FillColor = System.Drawing.Color.Red;
shape.StrokeColor = System.Drawing.Color.Yellow;

doc.SaveToFile("output.docx", FileFormat.Docx2013);
[VB.NET]
Dim doc As New Document()
Dim section As Section = doc.AddSection()
Dim paragraph As Paragraph = section.AddParagraph()

Dim shape As ShapeObject = paragraph.AppendShape(120, 30, ShapeType.TextWave4)
shape.VerticalPosition = 80
shape.HorizontalPosition = 170
shape.WordArt.Text = "Thanks for reading."
shape.FillColor = System.Drawing.Color.Red
shape.StrokeColor = System.Drawing.Color.Yellow

doc.SaveToFile("output.docx", FileFormat.Docx2013)
Thursday, 13 July 2017 02:33

Extract message contents in C#, VB.NET

This article illustrates how to get message contents such as from address, send to address, subject, date and the body of the message by using Spire.Email.

Code snippets of how to extract the message contents:

Step 1: Load the mail message.

MailMessage mail = MailMessage.Load("Sample.msg");

Step 2: Create a new instance of StringBuilder.

StringBuilder sb = new StringBuilder();

Step 3: Get the message contents as we want.

//get the From address
sb.AppendLine("From:");
sb.AppendLine(mail.From.Address);

//get the To address
sb.AppendLine("To:");
foreach (MailAddress toAddress in mail.To)
{
    sb.AppendLine(toAddress.Address);
}

//get the date
sb.AppendLine("Date:");
sb.AppendLine(mail.Date.ToString());

//get the subject
sb.AppendLine("Subject:");
sb.AppendLine(mail.Subject);

//get the BodyText
sb.AppendLine("Message contents");
sb.AppendLine(mail.BodyText);

//get the BodyHtml
sb.AppendLine("BodyHtml");
sb.AppendLine(mail.BodyHtml);

Step 4: Write all contents in .txt

File.WriteAllText("ExtractMessageContents.txt", sb.ToString());

The extracted message contents in .txt file format.

Extract message contents in C#, VB.NET

Full codes:

[C#]
{
    
    MailMessage mail = MailMessage.Load("Sample.msg");

    StringBuilder sb = new StringBuilder();

    sb.AppendLine("From:");
    sb.AppendLine(mail.From.Address);

    sb.AppendLine("To:");
    foreach (MailAddress toAddress in mail.To)
    {
        sb.AppendLine(toAddress.Address);
    }

    sb.AppendLine("Date:");
    sb.AppendLine(mail.Date.ToString());

    sb.AppendLine("Subject:");
    sb.AppendLine(mail.Subject);

    sb.AppendLine("Message contents");
    sb.AppendLine(mail.BodyText);

    sb.AppendLine("BodyHtml");
    sb.AppendLine(mail.BodyHtml);
        
    File.WriteAllText("ExtractMessageContents.txt", sb.ToString()); 
}
[VB.NET]
Dim mail As MailMessage = MailMessage.Load("Sample.msg")

Dim sb As New StringBuilder()

sb.AppendLine("From:")
sb.AppendLine(mail.From.Address)

sb.AppendLine("To:")
For Each toAddress As MailAddress In mail.[To]
	sb.AppendLine(toAddress.Address)
Next

sb.AppendLine("Date:")
sb.AppendLine(mail.[Date].ToString())

sb.AppendLine("Subject:")
sb.AppendLine(mail.Subject)

sb.AppendLine("Message contents")
sb.AppendLine(mail.BodyText)

sb.AppendLine("BodyHtml")
sb.AppendLine(mail.BodyHtml)

File.WriteAllText("ExtractMessageContents.txt", sb.ToString())
Wednesday, 12 July 2017 06:24

Send Bulk Emails in C#, VB.NET

Sending bulk emails means that you can send a batch of emails to multiple recipients and they will not be able to determine the others you've sent the message to.

The following code snippets demonstrate how to send bulk emails using Spire.Email in C# and VB.NET.

Step 1: Create instances of MailMessage class and specify sender and recipients.

MailMessage message1 = new MailMessage("sender@e-iceblue.com", "recipient1@e-iceblue.com");
MailMessage message2 = new MailMessage("sender@e-iceblue.com", " recipient2@e-iceblue.com");
MailMessage message3 = new MailMessage("sender@e-iceblue.com", " recipient3@e-iceblue.com");

Step 2: Set the subject and body text of the messages.

message1.Subject = message2.Subject = message3.Subject = "Subject";
message1.BodyText = message2.BodyText = message3.BodyText = "This is body text.";

Step 3: Initialize an object of MailMessageCollection class and add the instances of MailMessage class into the object.

List msgs = new List();
msgs.Add(message1);
msgs.Add(message2);
msgs.Add(message3);

Step 4: Create a SmtpClient instance with host, port, username and password, and send batch of emails using SendSome method.

SmtpClient client = new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = "sender@e-iceblue.com";
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendSome(msgs);
Console.WriteLine("Message sent");

Full Code:

[C#]
MailMessage message1 = new MailMessage("sender@e-iceblue.com", "recipient1@e-iceblue.com");
MailMessage message2 = new MailMessage("sender@e-iceblue.com", " recipient2@e-iceblue.com");
MailMessage message3 = new MailMessage("sender@e-iceblue.com", " recipient3@e-iceblue.com");
message1.Subject = message2.Subject = message3.Subject = "subject";
message1.BodyText = message2.BodyText = message3.BodyText = "This is body text.";

List msgs = new List();
msgs.Add(message1);
msgs.Add(message2);
msgs.Add(message3);

SmtpClient client = new SmtpClient();
client.Host = "smtp.outlook.com";
client.Port = 587;
client.Username = "sender@e-iceblue.com";
client.Password = "password";
client.ConnectionProtocols = ConnectionProtocols.Ssl;
client.SendSome(msgs);
Console.WriteLine("Message sent");
[VB.NET]
Dim message1 As New MailMessage("sender@e-iceblue.com", "recipient1@e-iceblue.com")
Dim message2 As New MailMessage("sender@e-iceblue.com", " recipient2@e-iceblue.com")
Dim message3 As New MailMessage("sender@e-iceblue.com", " recipient3@e-iceblue.com")
message1.Subject = InlineAssignHelper(message2.Subject, InlineAssignHelper(message3.Subject, "subject"))
message1.BodyText = InlineAssignHelper(message2.BodyText, InlineAssignHelper(message3.BodyText, "This is body text."))

Dim msgs As New List(Of MailMessage)()
msgs.Add(message1)
msgs.Add(message2)
msgs.Add(message3)

Dim client As New SmtpClient()
client.Host = "smtp.outlook.com"
client.Port = 587
client.Username = "sender@e-iceblue.com"
client.Password = "password"
client.ConnectionProtocols = ConnectionProtocols.Ssl
client.SendSome(msgs)
Console.WriteLine("Message sent")
Tuesday, 11 July 2017 06:51

Get Mailbox Information in C#, VB.NET

This article illustrates how to get mailbox information such as the number of messages, the size of mailbox and the unique id of a specific message using Spire.Email component.

Detail steps:

Step 1: Create a pop3 client.

Pop3Client pop = new Pop3Client();

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

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

Step 3: Connect the pop server.

pop.Connect();

Step 4: Get information of mailbox.

//Get the number of messages
Console.WriteLine("Mailbox message count: " + pop.GetMessageCount());
            
//Get the size of mailbox
Console.WriteLine("Mailbox size: " + pop.GetSize() + " bytes");

//Get the unique id of the first message
Console.WriteLine("Message uid: " + pop.GetMessagesUid(1));

Screenshot:

Get Mailbox Information in C#, VB.NET

Full code:

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

namespace Get_mailbox_information
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a pop3 client
            using (Pop3Client pop = new Pop3Client())
            {
                //Set host, authentication, port and connection protocol
                pop.Host = "outlook.office365.com";
                pop.Username = "LeonDavisLD@outlook.com";
                pop.Password = "password";
                pop.Port = 995;
                pop.EnableSsl = true;

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

                //Get the number of messages
                Console.WriteLine("Mailbox message count: " + pop.GetMessageCount());

                //Get the size of mailbox
                Console.WriteLine("Mailbox size: " + pop.GetSize() + " bytes");

                //Get the unique id of the first message
                Console.WriteLine("Message uid: " + pop.GetMessagesUid(1));
            }
        }
    }
}
[VB.NET]
Imports Spire.Email.Pop3

Namespace Get_mailbox_information
	Class Program
		Private Shared Sub Main(args As String())
			'Create a pop3 client
			Using pop As New Pop3Client()
				'Set host, authentication, port and connection protocol
				pop.Host = "outlook.office365.com"
				pop.Username = "LeonDavisLD@outlook.com"
				pop.Password = "password"
				pop.Port = 995
				pop.EnableSsl = True

				'Connect the pop server
				pop.Connect()

				'Get the number of messages
				Console.WriteLine("Mailbox message count: " + pop.GetMessageCount())

				'Get the size of mailbox
				Console.WriteLine("Mailbox size: " + pop.GetSize() + " bytes")

				'Get the unique id of the first message
				Console.WriteLine("Message uid: " + pop.GetMessagesUid(1))
			End Using
		End Sub
	End Class
End Namespace

Following code snippets demonstrate how to create, rename and delete a folder on mail server by using ImapClient class in Spire.Email.

Create a mail folder

//create an imapclient with username, password and host
ImapClient client = new ImapClient();       
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();     
//create a folder named ‘e-iceblue’
client.CreateFolder("e-iceblue");
Console.WriteLine("Done!");

Create, Rename and Delete Mail Folders using IMAP Client

Rename a mail folder

//create an imapclient with username, password and host
ImapClient client = new ImapClient();       
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();
//rename an existing folder with a new name       
client.RenameFolder("e-iceblue", "E-ICEBLUE");
Console.WriteLine("Done!");

Create, Rename and Delete Mail Folders using IMAP Client

Delete a mail folder

//create an imapclient with username, password and host
ImapClient client = new ImapClient();       
client.Username = "test@outlook.com";
client.Password = "password";
client.Host = "outlook.office365.com";
//specify the port
client.Port = 143;
//specify the connection protocol
client.ConnectionProtocols = ConnectionProtocols.Ssl;
//connect to imap mail server
client.Connect();
//delete an existing folder       
client.DeleteFolder("E-ICEBLUE");
Console.WriteLine("Done!");

Create, Rename and Delete Mail Folders using IMAP Client

Tuesday, 04 July 2017 06:06

Extract and delete Email attachment

This article demonstrates how to extract the attachment from an email message and delete the attachment via Spire.Email in C# and VB.NET. Spire.Email supports to work with MSG, EML, EMLX, MHTML, PST, OST and TNEF email file formats. On this article we use .msg message format for example.

Firstly, please view the sample email message with attachments:

Extract and delete Email attachment

How to extract the attachment from an Email message:

[C#]
//Load the mail message from file
MailMessage mail = MailMessage.Load("Test.msg");

//Create a folder named Attachments
if (!Directory.Exists("Attachments"))
{
    Directory.CreateDirectory("Attachments");
}

foreach (Attachment attach in mail.Attachments)
{
     //To get and save the attachment
    string filePath = string.Format("Attachments\\{0}", attach.ContentType.Name);
    if (File.Exists(filePath))
    {
        File.Delete(filePath);
    }
    FileStream fs = File.Create(filePath);
attach.Data.CopyTo(fs);
}
[VB.NET]
'Load the mail message from file
Dim mail As MailMessage = MailMessage.Load("Test.msg")

'Create a folder named Attachments
If Not Directory.Exists("Attachments") Then
    Directory.CreateDirectory("Attachments")
End If

For Each attach As Attachment In mail.Attachments
    'To get and save the attachment
    Dim filePath As String = String.Format("Attachments\{0}", attach.ContentType.Name)
    If File.Exists(filePath) Then
        File.Delete(filePath)
    End If
    Dim fs As FileStream = File.Create(filePath)
    attach.Data.CopyTo(fs)
Next

Extract and delete Email attachment

How to delete the attachment from an Email message:

[C#]
MailMessage mail = MailMessage.Load("Test.msg");
// Delete the attachment by index
mail.Attachments.RemoveAt(0);
// Delete the attachment by attachment name
for (int i = 0; i < mail.Attachments.Count; i++)
{
    Attachment attach = mail.Attachments[i];
    if (attach.ContentType.Name == "logo.png")
    {
        mail.Attachments.Remove(attach);
    }
}
mail.Save("HasDeletedAttachment.msg", MailMessageFormat.Msg);
[VB.NET]
Private Shared Sub Main(args As String())
    Dim mail As MailMessage = MailMessage.Load("Test.msg")
    ' Delete the attachment by index
    mail.Attachments.RemoveAt(0)
    ' Delete the attachment by attachment name
    For i As Integer = 0 To mail.Attachments.Count - 1
        Dim attach As Attachment = mail.Attachments(i)
        If attach.ContentType.Name = "logo.png" Then
            mail.Attachments.Remove(attach)
        End If
    Next
    mail.Save("HasDeletedAttachment.msg", MailMessageFormat.Msg)
End Sub

Extract and delete Email attachment

Monday, 03 July 2017 08:00

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

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]
Dim addressFrom As MailAddress =  New MailAddress("jack.du@e-iceblue.com","Jack Du") 
Dim addressTo As MailAddress =  New MailAddress("susanwong32@outlook.com") 
Dim message As MailMessage =  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
 
Dim client As SmtpClient =  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()