Add Message to PST File in C#, VB.NET
In the previous article, we have introduced how to read a PST file and get the folder information from it. This article will show you how we can add existing mail message files into PSF file for archiving.
Step 1: Load a PST file from disk into an instance of OutlookFile class.
OutlookFile outlookFile = new OutlookFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.pst");
Step 2: Load a MSG file into an instance of OutlookItme class.
OutlookItem item = new OutlookItem(); item.LoadFromFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.msg");
Step 3: Get inbox folder from PST file.
OutlookFolder inboxFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox");
Step 4: Add the MSG file to inbox folder.
inboxFolder.AddItem(item);
Full Code:
using Spire.Email; using Spire.Email.Outlook; using System; namespace AddMessageToPSTFile { class Program { static void Main(string[] args) { OutlookFile outlookFile = new OutlookFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.pst"); OutlookItem item = new OutlookItem(); item.LoadFromFile(@"C:\Users\Administrator\Documents\Outlook Files\Sample.msg"); OutlookFolder inboxFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox"); inboxFolder.AddItem(item); Console.WriteLine("Completed"); } } }
Imports Spire.Email Imports Spire.Email.Outlook Namespace AddMessageToPSTFile Class Program Private Shared Sub Main(args As String()) Dim outlookFile As New OutlookFile("C:\Users\Administrator\Documents\Outlook Files\Sample.pst") Dim item As New OutlookItem() item.LoadFromFile("C:\Users\Administrator\Documents\Outlook Files\Sample.msg") Dim inboxFolder As OutlookFolder = outlookFile.RootOutlookFolder.GetSubFolder("Inbox") inboxFolder.AddItem(item) Console.WriteLine("Completed") End Sub End Class End Namespace
Creating MSG Files with RTF body and attachment
Spire.Email supports to create Outlook message (MSG) files with rich text bodies and attachments in C# and VB.NET. The RTF body supports to set the formatting for the font, such as in bold, underline and set the color and add lists to the message body. It makes the outlook message vivid and clear to view. Here comes to the steps of how to use Spire.Email to create and save the Outlook message with RTF body and attachment in C#.
Step 1: Create an instance of MailMessage class and specify sender and recipient address.
MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang"); MailAddress addressTo = new MailAddress("susanwong32@outlook.com"); MailMessage mail = new MailMessage(addressFrom, addressTo);
Step 2: Add the subject, message body and attachment of the message.
mail.Subject = "This is a test message"; string htmlString = @" <p>Dear Ms. Susan,</p> <p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p> <ul> <li> Create a message with RTF file </li> <li> Create a message with attachment</li> </ul> <p style='color:red'>This text is in red </p> <p>Best regards, </p> <p>Daisy </p>"; mail.BodyHtml = htmlString; mail.Attachments.Add(new Attachment("logo.png"));
Step 3: Save the message as MSG format.
mail.Save("Sample.msg", MailMessageFormat.Msg);
Effective screenshot:
Full codes:
using Spire.Email; namespace CreatingMSGFiles { class Program { static void Main(string[] args) { MailAddress addressFrom = new MailAddress("daisy.zhang@e-iceblue.com", "Daisy Zhang"); MailAddress addressTo = new MailAddress("susanwong32@outlook.com"); MailMessage mail = new MailMessage(addressFrom, addressTo); mail.Subject = "This is a test message"; string htmlString = @" <p>Dear Ms. Susan,</p> <p>This is an example of creating an <b>outlook message</b> <u>(msg)</u>.</p> <ul> <li> Create a message with RTF file </li> <li> Create a message with attachment</li> </ul> <p style='color:red'>This text is in red </p> <p>Best regards, </p> <p>Daisy </p>"; mail.BodyHtml = htmlString; mail.Attachments.Add(new Attachment("logo.png")); mail.Save("Sample.msg", MailMessageFormat.Msg); } } }
Get Folder Information from PST File in C#, VB.NET
The PST files are used to store information that pertains to the e-mail folders, addresses, contact information, email messages and other data that is saved within Outlook and Exchange programs. Spire.Email supports to read PST files and get the folder information such as folder name, message count and unread message count.
Step 1: Load a PST file from disk into an instance of OutlookFile class.
OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst");
Step 2: Get the folders collection.
OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders();
Step 3: Traverse the collection and get the folder information of each element in the collection.
foreach (OutlookFolder folder in folderCollection) { Console.WriteLine("Folder: " + folder.Name); Console.WriteLine("Total items: " + folder.ItemCount); Console.WriteLine("Total unread items: " + folder.UnreadItemCount); Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders)?"Yes":"No"); Console.WriteLine("------------------Next Folder--------------------"); }
Output:
Full Code:
using Spire.Email; using Spire.Email.Outlook; using System; namespace GetFolderInformation { class Program { static void Main(string[] args) { OutlookFile olf = new OutlookFile(@"C:\Users\jack\Documents\Outlook Files\Sample.pst"); OutlookFolderCollection folderCollection = olf.RootOutlookFolder.GetSubFolders(); foreach (OutlookFolder folder in folderCollection) { Console.WriteLine("Folder: " + folder.Name); Console.WriteLine("Total items: " + folder.ItemCount); Console.WriteLine("Total unread items: " + folder.UnreadItemCount); Console.WriteLine("Whether this folder has subfolders: {0}", (folder.HasSubFolders) ? "Yes" : "No"); Console.WriteLine("------------------Next Folder--------------------"); } Console.WriteLine("Completed"); } } }
Imports Spire.Email Imports Spire.Email.Outlook Namespace GetFolderInformation Class Program Private Shared Sub Main(args As String()) Dim olf As New OutlookFile("C:\Users\jack\Documents\Outlook Files\Sample.pst") Dim folderCollection As OutlookFolderCollection = olf.RootOutlookFolder.GetSubFolders() For Each folder As OutlookFolder In folderCollection Console.WriteLine("Folder: " + folder.Name) Console.WriteLine("Total items: " + folder.ItemCount) Console.WriteLine("Total unread items: " + folder.UnreadItemCount) Console.WriteLine("Whether this folder has subfolders: {0}", If((folder.HasSubFolders), "Yes", "No")) Console.WriteLine("------------------Next Folder--------------------") Next Console.WriteLine("Completed") End Sub End Class End Namespace
Convert EML/EMLX to MHTML or MSG in C#, VB.NET
A file with the EMLX or EML file extension is a Mail Message file used to store an email message. EML/EMLX file can converted to MHTML or MSG file format with few lines of core code by using Spire.Email.
Convert EML/EMLX to MHTML
using Spire.Email; using System; namespace ConvertEMLandEMLXtoMHTML { class Program { static void Main(string[] args) { MailMessage message = MailMessage.Load("example.eml"); message.Save("ToMhtml.mhtml", MailMessageFormat.Mhtml); Console.WriteLine("Done"); } } }
Imports Spire.Email Namespace ConvertEMLandEMLXtoMHTML Class Program Private Shared Sub Main(args As String()) Dim message As MailMessage = MailMessage.Load("example.eml") message.Save("ToMhtml.mhtml", MailMessageFormat.Mhtml) Console.WriteLine("Done") End Sub End Class End Namespace
Convert EML/EMLX to MSG
using Spire.Email; using System; namespace ConvertEMLandEMLXtoMSG { class Program { static void Main(string[] args) { MailMessage message = MailMessage.Load("example.eml"); message.Save("ToMsg.msg", MailMessageFormat.Msg); Console.WriteLine("Done"); } } }
Imports Spire.Email Namespace ConvertEMLandEMLXtoMSG Class Program Private Shared Sub Main(args As String()) Dim message As MailMessage = MailMessage.Load("example.eml") message.Save("ToMsg.msg", MailMessageFormat.Msg) Console.WriteLine("Done") End Sub End Class End Namespace
Subscribe and Unsubscribe Folders in C#, VB.NET
Spire.Email supports to manage folder subscriptions by using ImapClient.Subscribe and ImapClient.Unsubscribe method.
The following example shows how to subscribe to a folder and unsubscribe from a folder 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: Subscribe folder using its name.
imap.Subscribe("Folder1");
Step 5: Unsubscribe folder.
imap.Unsubscribe("Folder2");
Screenshot:
Full code:
using Spire.Email; using Spire.Email.IMap; namespace SubscribAndUnsubscribeFolders { class Program { static void Main(string[] args) { //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(); //subscribe folder using its name imap.Subscribe("Folder1"); //Unsubscribe folder imap.Unsubscribe("Folder2"); } } }
Imports Spire.Email Imports Spire.Email.IMap Namespace SubscribAndUnsubscribeFolders Class Program Private Shared Sub Main(args As String()) '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() 'subscribe folder using its name imap.Subscribe("Folder1") 'Unsubscribe folder imap.Unsubscribe("Folder2") End Sub End Class End Namespace
Update the send to email address for Email message
When we operate the email messages, we may need to send the message to many more email address. Spire.Email supports to send a message to many email addresses at one time. This article demonstrates how to add the new send To and Cc email address from an existing email message via Spire.Email in C# and VB.NET.
Firstly, please view the sample email message with one to and cc email address:
How to add new send to and cc email addresses:
using Spire.Email; namespace Update { class Program { static void Main(string[] args) { //Load the mail message from file MailMessage mail = MailMessage.Load("Sample.msg"); //add a new To email address and its display name mail.To.Add(new MailAddress("support@e-iceblue.com", "E-iceblue Support")); // add a new Cc email address and its display name mail.Cc.Add(new MailAddress("comments@e-iceblue.com", "Comments")); // Save the message mail.Save("ChangeEmailAddress.msg", MailMessageFormat.Msg); } } }
Imports Spire.Email Namespace Update Class Program Private Shared Sub Main(args As String()) 'Load the mail message from file Dim mail As MailMessage = MailMessage.Load("Sample.msg") 'add a new To email address and its display name mail.[To].Add(New MailAddress("support@e-iceblue.com", "E-iceblue Support")) ' add a new Cc email address and its display name mail.Cc.Add(New MailAddress("comments@e-iceblue.com", "Comments")) ' Save the message mail.Save("ChangeEmailAddress.msg", MailMessageFormat.Msg) End Sub End Class End Namespace
Effective screenshot after changing the email address:
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:
Full code:
using Spire.Email; using Spire.Email.IMap; using System; namespace SearchEmailMessages { class Program { static void Main(string[] args) { //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); } } }
Imports Spire.Email Imports Spire.Email.IMap Namespace SearchEmailMessages Class Program Private Shared Sub Main(args As String()) '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) End Sub End Class End Namespace
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);
Full code:
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); } } }
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
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.
Full codes:
using Spire.Email; using System.IO; using System.Text; namespace ExtractMessage { class Program { static void Main(string[] args) { 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()); } } }
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())
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:
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");
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")