Extracting Messages from Outlook PST and Saving them to MSG

VSTO

string pstFilePath = "sample.pst";
            Outlook.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["sample"].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);


Aspose.Email

string pstFilePath ="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");
                    }


Download

Last edited Jan 1, 2014 at 6:37 AM by asposemarketplace, version 2