Add Message to PST File in C#, VB.NET

2017-08-18 07:27:29 Written by  support iceblue
Rate this item
(0 votes)

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

Additional Info

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