Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The Outlook data file (.pst) failed to load for this session

Tags:

c#

outlook

i am using Microsoft.Office.Interop.Outlook Version 12.0.0.0 to read my outlook pst file but when compiler reaches this code outlookNs.AddStore(pstFilePath); it gives exception that "The Outlook data file (.pst) failed to load for this session." i have tried outlookNs.AddStoreEx(pstFilePath); also but the error was same ....any sugession ??

using System;
using System.Collections.Generic;
using Microsoft.Office.Interop.Outlook;

namespace PSTReader {
    class Program {
        static void Main () {
            try {
                IEnumerable<MailItem> mailItems = readPst(@"C:\temp\PST\Test.pst", "Test PST");
                foreach (MailItem mailItem in mailItems) {
                    Console.WriteLine(mailItem.SenderName + " - " + mailItem.Subject);
                }
            } catch (System.Exception ex) {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }

        private static IEnumerable<MailItem> readPst(string pstFilePath, string pstName) {
            List<MailItem> mailItems = new List<MailItem>();
            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[pstName].GetRootFolder();
            // Traverse through all folders in the PST file
            // TODO: This is not recursive, refactor
            Folders subFolders = rootFolder.Folders;
            foreach (Folder folder in subFolders) {
                Items items = folder.Items;
                foreach (object item in items) {
                    if (item is MailItem) {
                        MailItem mailItem = item as MailItem;
                        mailItems.Add(mailItem);
                    }
                }
            }
            // Remove PST file from Default Profile
            outlookNs.RemoveStore(rootFolder);
            return mailItems;
        }
    }
}
like image 461
RameezAli Avatar asked Mar 05 '14 07:03

RameezAli


People also ask

Can't import PST file into Outlook?

To fix: right-click the . pst file, choose Properties > Security > Edit > choose your Microsoft account, and choose Full control. Now restart Outlook.

What causes PST file corruption?

A PST file may be corrupted if it is open in Outlook when the application terminates abnormally. This can happen if either Outlook or Windows crashes, or if the machine is powered down without quitting Outlook and Windows normally.


1 Answers

I got the same issue, and the below is what I did.

When you are saying outlookNs.AddStore, you have to give path and the file name. and then in the outlookNs.Stores, the variable pstName should have any extension as .pst, you have to remove it.

Below is the sample on how I got it worked.

public class Mail
{
    public MailItem mailItem { get; set; }
    public String path { get; set; }
}


public static class Mails
{
    public static List<Mail> readPst(string pstFilePath, string pstName)
    {
        List<Mail> mail = new List<Mail>();
        Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
        NameSpace outlookNs = app.GetNamespace("MAPI");

        // Add PST file (Outlook Data File) to Default Profile
        outlookNs.AddStore(pstFilePath + pstName);

        string storeInfo = null;

        foreach (Store store in outlookNs.Stores)
        {
            storeInfo = store.DisplayName;
            storeInfo = store.FilePath;
            storeInfo = store.StoreID;
        }

        MAPIFolder rootFolder = outlookNs.Stores[pstName.Substring(0,pstName.Length-4)].GetRootFolder();

        // Traverse through all folders in the PST file
        Folders subFolders = rootFolder.Folders;

        foreach (Folder folder in subFolders)
        {
            ExtractItems(mail, folder);
        }
        // Remove PST file from Default Profile
        outlookNs.RemoveStore(rootFolder);
        return mail;
    }

    private static void ExtractItems(List<Mail> mailItems, Folder folder)
    {
        Items items = folder.Items;

        int itemcount = items.Count;

        foreach (object item in items)
        {
            if (item is MailItem)
            {
                MailItem mailItem = item as MailItem;
                Mail mail = new Mail();
                mail.mailItem = mailItem;
                mail.path = folder.FolderPath + folder.Name;
                mailItems.Add(mail);
            }
        }

        foreach (Folder subfolder in folder.Folders)
        {
            ExtractItems(mailItems, subfolder);
        }
    }
}
like image 195
Surendra Avatar answered Oct 23 '22 23:10

Surendra