Mbox Storage Files

Importing EML messages to a PST file is a common requirement, especially for users migrating between email clients or archiving emails for storage and backup purposes. The PST format preserves the structure, formatting, and attachments of the content, making it suitable for seamless integration and management within the Outlook application. The primary advantage of this proprietary Microsoft file format is the capability to create local archives of email data, making it possible to manage and access emails, contacts, and other related information offline. In this article, we will explore how to import EML to PST easily and efficiently using a progressive C# library with code samples and steps.

.NET API to Manage EML

Aspose.Email for .NET is a robust and versatile API that offers a wide range of features for working with email messages and mail servers in .NET applications. The API provides seamless integration with popular email protocols and formats, enabling developers to create and manipulate email messages with ease.

One of the notable features of Aspose.Email is its extensive support for converting emails between different formats, including transferring EML to PST. By leveraging the rich set of classes and methods provided by Aspose.Email, developers can perform complex email-related tasks, such as handling attachments, managing folders, and converting emails between various formats. Before diving into the code, you’ll need to integrate the library into your C# project. You can easily obtain it by downloading from the Aspose website or by using NuGet.

Install-Package Aspose.Email

Once the library is a part of your project, you’re ready to start coding.

Import a Single EML Message to a PST File

Aspose.Email comprehensive API provides straightforward and intuitive code sample and steps to add an EML message to a PST file:

  1. Create a new personal storage file using the Create method of the PersonalStorage class specifying the file name and format version as parameters.
  2. Create a predefined “Inbox” folder within the PST file using the CreatePredefinedFolder method of the PersonalStorage class, specifying “Inbox” as the folder name and StandardIpmFolder.Inbox as the folder type.
  3. Load an email message from a file using the ‘Load’ method of the MapiMessage class, specifying its name and an instance of EmlLoadOptions as parameters.
  4. Add the loaded email message to the previously created Inbox folder within the PST file using the AddMessage method of the FolderInfo class.

As a result, a new Personal Storage file named “test.pst” is created, the message loaded from the file “test.eml” is added to the Inbox folder, and is available for further manipulation or storage.

Import Multiple EML Messages to a PST File

Aspose.Email makes it possible to add a set of messages from a folder to a PST file. The AddMessages method of the FolderInfo class allows you to import more than one EML message into the Inbox folder within a storage file. The following code sample will show you how to implement this feature into your project:

  1. Create a new personal storage file using the Create method of the PersonalStorage class specifying the file name and format version as parameters.
  2. Create a predefined “Inbox” folder within the PST file using the CreatePredefinedFolder method of the PersonalStorage class, specifying “Inbox” as the folder name and StandardIpmFolder.Inbox as the folder type.
  3. Add one or more email messages from a folder to the previously created Inbox folder within the PST file using the AddMessages method of the FolderInfo class, specifying a MapiMessageEnumerator object that represents the messages to be imported from the specified folder path.

Here, we leverage the ‘MapiMessageEnumerator’ to iterate over the EML files in the source folder and add them to the PST file.

MapiMessage Enumerator Implementation

With Aspose.Email, you can define the ‘MapiMessageEnumerator’ class that implements IEnumerable interface. This class allows iterating through a collection of MapiMessage objects, representing email messages, stored as .eml files in a specified folder. The Enumerator nested class within MapiMessageEnumerator serves as the iterator, performing the loading of EML files as MapiMessage objects and managing the iteration process. It has methods for iterating through the files, disposing of resources, and resetting the enumerator.

The following code sample will show you how to iterate through a collection of MapiMessages programmatically:

  1. Define the ‘MapiMessageEnumerator’ class accepting a ‘folderPath’ parameter, which indicates the directory where .eml files are located.
  2. Create and return a new instance of the ‘Enumerator’ class using the ‘GetEnumerator’ method, passing the folder path.
  3. Store the ‘folderPath’ in a field and use the ‘IEnumerator’ to iterate through the file paths within the folder.
  4. When the ‘Enumerator’ class is constructed, initialize the ‘folderPath’ and call the ‘Reset()’ method to prepare the ‘fileEnumerator’.
  5. Implement the ‘Current’ property and IEnumerator’s Current property explicitly.
  6. Provide a ‘Dispose’ method to dispose of the ‘fileEnumerator’ and the current ‘MapiMessage’ if they exist.
  7. Use the ‘MoveNext’ method to iterate through the file paths, attempt to load a ‘MapiMessage’ from the file, and log or handle any errors that occur.
  8. In the ‘Reset’ method, dispose of the ‘fileEnumerator’ and create a new one by enumerating the .eml files within the ‘folderPath’.

Conclusion

In conclusion, whether you are migrating between email clients or archiving email data for storage and backup purposes, Aspose.Email for .NET provides a reliable and feature-rich solution for transferring EML files to PST format making it an efficient and straightforward process. With intuitive code samples and comprehensive documentation, developers can effortlessly implement this essential functionality into their projects. Additionally, the free forum offers a community-driven support system for any questions or issues that may arise during implementation. To get more information about the API high code features, please visit our reference resources.

See Also