How to Extract Email Messages from Outlook PST & Save to MSG inside .NET Apps
This technical tip explains how .NET developers can extract email messages from outlook PST and save them to MSG format. To extract messages from an Outlook PST file and save them to disk as MSG files. It involves several steps such as
<br/>
<br/>
• Read the Outlook PST file,
<br/>
• extract messages and, finally,
<br/>
• save the extracted messages.
<br/>
<br/>
There are different ways to achieve the same result: this article compares using VSTO and Aspose.Email. First, are code samples for using Microsoft Office Interop to extract messages from PST. After that example, code samples show how to use Aspose.Email.Outlook, in both C# and VB.NET, to perform the same task.
<br/>
//your code here...Using Microsoft Office Interop
//To use Office Automation objects for Microsoft Outlook, add references to Microsoft Office Interop for Outlook libraries to the project. Microsoft Office Outlook must also be installed on the machine that the code runs on. The namespace used in the code sample that follows is Microsoft.Office.Interop.Outlook.
//[C# Code Sample]
string pstFilePath = @"C:\sample.pst";
Application app = new Application();
NameSpace outlookNs = app.GetNamespace("MAPI");
// Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath);
MAPIFolder rootFolder = outlookNs.Stores["items"].GetRootFolder();
// Traverse through all folders in the PST file
// TODO: This is not recursive
Folders subFolders = rootFolder.Folders;
foreach (Folder folder in subFolders)
{
Items items = folder.Items;
foreach (object item in items)
{
if (item is MailItem)
{
// Retrieve the Object into MailItem
MailItem mailItem = item as MailItem;
Console.WriteLine("Saving message {0} ....", mailItem.Subject);
// Save the message to disk in MSG format
// TODO: File name may contain invalid characters [\ / : * ? " < > |]
mailItem.SaveAs(@"\extracted\" + mailItem.Subject + ".msg",OlSaveAsType.olMSG);
}
}
}
// Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder);
//[VB.NET Code Sample]
Dim pstFilePath As String = "C:\sample.pst"
Dim app As New Application()
Dim outlookNs As [NameSpace] = app.GetNamespace("MAPI")
' Add PST file (Outlook Data File) to Default Profile
outlookNs.AddStore(pstFilePath)
Dim rootFolder As MAPIFolder = outlookNs.Stores("items").GetRootFolder()
' Traverse through all folders in the PST file
' TODO: This is not recursive
Dim subFolders As Folders = rootFolder.Folders
For Each folder As Folder In subFolders
Dim items As Items = folder.Items
For Each item As Object In items
If TypeOf item Is MailItem Then
' Retrieve the Object into MailItem
Dim mailItem As MailItem = TryCast(item, MailItem)
Console.WriteLine("Saving message {0} ....", mailItem.Subject)
' Save the message to disk in MSG format
' TODO: File name may contain invalid characters [\ / : * ? " < > |]
mailItem.SaveAs("\extracted\" & mailItem.Subject & ".msg", OlSaveAsType.olMSG)
End If
Next item
Next folder
' Remove PST file from Default Profile
outlookNs.RemoveStore(rootFolder)
//Using Aspose.Email
//The following code snippets do the same thing as the code above but uses Aspose.Email. With Aspose.Email for .NEt installed Microsoft Outlook is no longer needed on the machine. Just reference the Aspose.Email.dll to build and run the project successfully. The namespaces used in the code samples that follows are Aspose.Email.Outlook.Pst and Aspose.Email.Outlook.
//[C# Code Sample]
string pstFilePath = @"C:\sample.pst";
// Create an instance of PersonalStorage and load the PST from file
using (PersonalStorage personalStorage = PersonalStorage.FromFile(pstFilePath))
{
// Get the list of subfolders in PST file
FolderInfoCollection folderInfoCollection = personalStorage.RootFolder.GetSubFolders();
// Traverse through all folders in the PST file
// TODO: This is not recursive
foreach (FolderInfo folderInfo in folderInfoCollection)
{
// Get all messages in this folder
MessageInfoCollection messageInfoCollection = folderInfo.GetContents();
// Loop through all the messages in this folder
foreach (MessageInfo messageInfo in messageInfoCollection)
{
// Extract the message in MapiMessage instance
MapiMessage message = personalStorage.ExtractMessage(messageInfo);
Console.WriteLine("Saving message {0} ....", message.Subject);
// Save the message to disk in MSG format
// TODO: File name may contain invalid characters [\ / : * ? " < > |]
message.Save(@"\extracted\" + message.Subject + ".msg");
}
}
}
//[VB.NET Code Sample]
Dim pstFilePath As String = "C:\sample.pst"
' Create an instance of PersonalStorage and load the PST from file
Using personalStorage As PersonalStorage = personalStorage.FromFile(pstFilePath)
' Get the list of subfolders in PST file
Dim folderInfoCollection As FolderInfoCollection = personalStorage.RootFolder.GetSubFolders()
' Traverse through all folders in the PST file
' TODO: This is not recursive
For Each folderInfo As FolderInfo In folderInfoCollection
' Get all messages in this folder
Dim messageInfoCollection As MessageInfoCollection = folderInfo.GetContents()
' Loop through all the messages in this folder
For Each messageInfo As MessageInfo In messageInfoCollection
' Extract the message in MapiMessage instance
Dim message As MapiMessage = personalStorage.ExtractMessage(messageInfo)
Console.WriteLine("Saving message {0} ....", message.Subject)
' Save the message to disk in MSG format
' TODO: File name may contain invalid characters [\ / : * ? " < > |]
message.Save("\extracted\" & message.Subject & ".msg")
Next messageInfo
Next folderInfo
End Using
Url: http://www.aspose.com/.net/email-component.aspx
Language: C# | User: Sheraz Khan | Created: Apr 8, 2015 | Tags: Read the Outlook PST extract Outlook PST messages save messages to MSG format Email Messages to MSG Outlook PST to MSG C# extract messages from PST .NET Outlook API Office Automation objects