Home > Code > VB.NET > How to Extract Email Messages from Outlook PST & Save to MSG inside .NET Apps

How to Extract Email Messages from Outlook PST & Save to MSG inside .NET Apps

by sher azam   on Apr 08, 2015   Category: VB.NET   |  Views: 2083    |  Points: 25   |  Gold 


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

• Read the Outlook PST file,
• extract messages and, finally,
• save the extracted messages.

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.

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



Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts