【问题标题】:The Outlook data file (.pst) failed to load for this session未能为此会话加载 Outlook 数据文件 (.pst)
【发布时间】:2014-04-07 02:39:45
【问题描述】:

我正在使用 Microsoft.Office.Interop.Outlook 版本 12.0.0.0 来读取我的 Outlook pst 文件,但是当编译器到达此代码 outlookNs.AddStore(pstFilePath); 时,它会给出异常 “未能为此会话加载 Outlook 数据文件 (.pst)。”我也试过outlookNs.AddStoreEx(pstFilePath);,但错误是一样的......有什么建议吗??

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;
        }
    }
}

【问题讨论】:

    标签: c# outlook


    【解决方案1】:

    在我的情况下,凭据都不是问题,也不是 PST 文件是在另一个进程中打开的。我不知道是什么原因造成的,但是...

    ... 我的解决方案是打开 Outlook->New Items->More Items->Outlook Data File...

    在弹出窗口中,选择 Outlook 数据文件 (.pst) 并点击确定

    这将打开 Windows Expolorer,导航到您的 PST 曾经创建的位置,以您以前的命名方式命名您的 PST 文件并将其保存到该位置。确保保存为 Outlook 数据文件 (*.pst) 类型。

    重启 Outlook

    这应该会重新启动 Outlook,并且您的 PST 现在应该已加载。在此之后,我还能够从我的 PST 所在位置(包括 PST)中删除所有内容,并且重新启动 Outlook 不会创建它而没有任何问题。

    【讨论】:

      【解决方案2】:

      我今天遇到了这个问题!高低搜索了一个没有成功的答案。

      最终,我们发现我的用户登录凭据对包含 PST 文件的文件夹结构具有读取权限,而我组织中的另一个用户没有。她收到上述错误,直到我们更改目录的权限以允许读取访问,问题才消失。

      【讨论】:

        【解决方案3】:

        是否可能 PST 是只读的或已在另一个进程中打开? Outlook 需要 PST 文件的读写权限,无论您打算如何处理它。

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题,下面是我所做的。

          当您说outlookNs.AddStore 时,您必须提供路径和文件名。 然后在 outlookNs.Stores 中,变量 pstName 应该有任何扩展名为 .pst,您必须将其删除。

          以下是我如何使用它的示例。

          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);
                  }
              }
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-01-09
            • 1970-01-01
            • 2017-02-17
            • 1970-01-01
            • 1970-01-01
            • 2020-12-20
            • 1970-01-01
            • 2023-03-30
            相关资源
            最近更新 更多