Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active June 30, 2023 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/92dc646b5526487c8490842727bc5a34 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/92dc646b5526487c8490842727bc5a34 to your computer and use it in GitHub Desktop.
Read MS Outlook PST Files in C# .NET | Read Emails and Contacts
// Load the Outlook PST file
PersonalStorage personalStorage = PersonalStorage.FromFile("SampleContacts.pst");
// Get the Contacts folder
FolderInfo folderInfo = personalStorage.RootFolder.GetSubFolder("Contacts");
// Loop through all the contacts in this folder
MessageInfoCollection messageInfoCollection = folderInfo.GetContents();
foreach (MessageInfo messageInfo in messageInfoCollection)
{
// Get the contact information
MapiMessage mapi = personalStorage.ExtractMessage(messageInfo);
// Cast to MapiContact
MapiContact contact = (MapiContact)mapi.ToMapiMessageItem();
// Display some contents on screen
Console.WriteLine("Name: " + contact.NameInfo.DisplayName);
// Save to disk in MSG format
if (contact.NameInfo.DisplayName != null)
{
MapiMessage message = personalStorage.ExtractMessage(messageInfo);
// Get rid of illegal characters that cannot be used as a file name
string messageName = message.Subject.Replace(":", " ").Replace("\\", " ").Replace("?", " ").Replace("/", " ");
message.Save(dataDir + "Contacts\\" + messageName + "_out.msg");
}
}
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.FromFile("PersonalStorage.pst");
// Get the Display Format of the PST file
Console.WriteLine("Display Format: " + pst.Format);
// Get the folders and messages information
Aspose.Email.Storage.Pst.FolderInfo folderInfo = pst.RootFolder;
// Call the recursive method to extract msg files from each folder
ExtractMsgFiles(folderInfo, pst);
/// <summary>
/// This is a recursive method to display contents of a folder
/// </summary>
/// <param name="folderInfo"></param>
/// <param name="pst"></param>
private static void ExtractMsgFiles(Aspose.Email.Storage.Pst.FolderInfo folderInfo, PersonalStorage pst)
{
// Display the folder name
Console.WriteLine("Folder: " + folderInfo.DisplayName);
Console.WriteLine("==================================");
// Loop through all the messages in this folder
Aspose.Email.Storage.Pst.MessageInfoCollection messageInfoCollection = folderInfo.GetContents();
foreach (Aspose.Email.Storage.Pst.MessageInfo messageInfo in messageInfoCollection)
{
Console.WriteLine("Saving message {0} ....", messageInfo.Subject);
// Get the message in MapiMessage instance
MapiMessage message = pst.ExtractMessage(messageInfo);
// Save this message to disk in msg format
message.Save(message.Subject.Replace(":", " ") + ".msg");
// Save this message to stream in msg format
MemoryStream messageStream = new MemoryStream();
message.Save(messageStream);
}
// Call this method recursively for each subfolder
if (folderInfo.HasSubFolders == true)
{
foreach (Aspose.Email.Storage.Pst.FolderInfo subfolderInfo in folderInfo.GetSubFolders())
{
ExtractMsgFiles(subfolderInfo, pst);
}
}
}
// Load PST file
PersonalStorage personalStorage = PersonalStorage.FromFile("PersonalStorage.pst");
// Get the folders information
FolderInfoCollection folderInfoCollection = personalStorage.RootFolder.GetSubFolders();
// Browse through each folder to display its information
foreach (FolderInfo folderInfo in folderInfoCollection)
{
Console.WriteLine("Folder: " + folderInfo.DisplayName);
Console.WriteLine("Total items: " + folderInfo.ContentCount);
Console.WriteLine("Total unread items: " + folderInfo.ContentUnreadCount);
Console.WriteLine("-----------------------------------");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment