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:
[C#]
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"); } } }
[VB.NET]
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