Create PST Files in C#

PST (Personal Storage Table) files serve as a vital component for managing Outlook data, facilitating storage for emails, contacts, calendars, and other items. In the realm of C# .NET development, the process of creating and managing storage files is crucial for various applications.

In this article, we’ll explore how to create PST files using C# .NET. We’ll cover the creation of empty PST storage, adding folders and subfolders, incorporating standard and regular folders, and finally, adding emails and Outlook items to the PST file.

C# API to Create and Populate PST Files

To work with storage files in PST format in C#, we will use Aspose.Email for .NET. This comprehensive library is designed for .NET developers, offering powerful tools for processing email-related data. With its intuitive APIs, developers can effortlessly create, read, and manipulate PST storage structures, including adding folders, subfolders, and standard Outlook folders. The library simplifies email message processing by providing functionalities for parsing, creating, sending, receiving, and converting emails across various formats like MSG, EML, and MHTML. Aspose.Email empowers developers to efficiently manage email-related tasks within their .NET applications, enhancing functionality and performance while delivering superior email processing capabilities to end-users.

To leverage the power of the API, you can either download its DLL or install it from NuGet using the following command:

PM> Install-Package Aspose.Email

Creating a Minimum Empty PST Storage

To start, let’s create an empty personal storage. This involves initializing a PST file without any items. The code sample below will show you how to set up the basic structure for a new file named “sample.pst” with Unicode file format version. To create a new PST file, use the PersonalStorage.Create method.

Note: the minimum pst is 256 Kb, and contains a root folder and a Deleted Items folder.

Adding Folders and Subfolders

Once we have the empty PST storage, the next step is to add folders and subfolders to organize our data efficiently. Consider the following subsections and try the following code samples to perform the task.

Adding Standard Folders

Adding standard folders like Inbox, Outbox, Sent Items, Drafts, etc., is straightforward with Aspose.Email. The following code sample demonstrates how to create a new folder called “Inbox” within a PST named “sample.pst”. The code uses the PersonalStorage class to open the PST file and then calls the CreatePredefinedFolder() method to create a standard folder for inbound items. The folder is created using the predefined folder type StandardIpmFolder.inbox, which typically represents the Inbox folder in email clients.

  1. Initialize a new PersonalStorage object.
  2. Use the CreatePredefinedFolder method of the PersonalStorage instance (pst) to create a standard folder named “Inbox” specifying the folder type StandardIpmFolder.Inbox to indicate that it is an Inbox folder.

The following list comprises the standard folders that can be added to a new PST file:

  • Inbox
  • Sent Items
  • Calendar
  • Contacts
  • Journal
  • Notes
  • Tasks
  • Sync issues
  • Junk emails
  • RSS feeds

Adding Regular Folders

In addition to standard folders, users often create custom folders with specific names for better organization of their emails. The following code snippet shows how to add a custom folder to the root of the PST file.

  1. Initialize a new PersonalStorage object.
  2. Use the AddSubFolder method of the RootFolder property of the PersonalStorage instance to create a custom folder.

After creating the “Custom Folder”, you can use the AddSubFolder method again to create subfolders within it. Provide the desired name for the subfolder as a parameter to the AddSubFolder method.

Adding Emails and Outlook Items

With the structure in place, we can now populate our PST file with emails and other Outlook items. This involves creating emails, appointments, contacts, tasks, and more, and adding them to the appropriate folders within the file. Let’s consider a couple of examples.

Adding New Emails

The code sample below will show you how to add a new email message to the “Inbox” folder within the PST file. The code involves the following steps:

  1. Initialize a new PersonalStorage object.
  2. Retrieve the Inbox folder by using the GetSubFolder method on the RootFolder property of the PersonalStorage instance.
  3. Instantiate a new MapiMessage object.
  4. Use the AddMessage method on the inboxFolder object to add the newly created message (message) to the Inbox folder.

Adding Outlook Items

The following code sample allows you to add a contact to a new PST file as an example. It outlines the process of creating a new personal storage file, adding a standard Contacts folder to it, loading a contact from a VCF file, and adding the contact to the Contacts folder. The code involves the following steps:

  1. Initialize a new PersonalStorage object.
  2. Use the CreatePredefinedFolder method to create a standard folder specifying the folder type StandardIpmFolder.Contacts to indicate that it is a Contacts folder.
  3. Use the MapiContact.FromVCard method to load a contact from a VCF file. This method creates a MapiContact object representing the contact information.
  4. Use the AddMapiMessageItem method to add the loaded contact to the Contacts folder.

Conclusion

In this tutorial, we’ve explored how to create PST files using C# .NET with the Aspose.Email library. From creating empty PST storage to adding folders, subfolders, and various Outlook items, the library provides a comprehensive set of features for PST file manipulation. Integrating these functionalities into your C# .NET applications empowers you to efficiently manage Outlook data, enhancing the robustness and versatility of your software solutions.

Apart from that, the library offers a wide range of features for creating, extracting, parsing, converting, and manipulating emails supported by comprehensive documentation and code examples to streamline the integration process, making it easy for developers to implement email-related solutions within their .NET applications.

Along with the advanced API, you can get continuous access to free resources:

  • support forum - a huge community to obtain answers to all of your questions
  • blog - invaluable information about up-to-date features
  • documentation - tutorials, code examples, and best practices with access to API references
  • free apps to view or convert, merge or parse emails, analyze headers, add watermark, and more

Aspose.Email ensures that developers have the resources needed to navigate any challenges and maximize the potential of the API within their applications.

See Also