我可以在C#中读取Outlook(2003/2007)PST文件吗?

是否有可能使用C#读取.PST文件? 我想做一个独立的应用程序,而不是作为一个Outlook插件(如果可能的话)。

如果看到其他 类似这样的问题提到MailNavigator,但我期待以编程方式在C#中执行此操作。

我已经看了Microsoft.Office.Interop.Outlook命名空间,但似乎只是为Outlook插件。 LibPST似乎能够读取PST文件,但这是在C(对不起,乔尔,我没有学过C之前,gradle )。

任何帮助将不胜感激,谢谢!

编辑:

谢谢大家的回应! 我接受了Matthew Ruston的回答,因为它最终将我引向了我正在寻找的代码。 这里是我工作的一个简单的例子(你将需要添加一个对Microsoft.Office.Interop.Outlook的引用):

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

注意:此代码假定Outlook已安装并已为当前用户configuration。 它使用默认configuration文件(您可以通过转到控制面板中的邮件编辑默认configuration文件)。 这个代码的一个主要改进是创build一个临时的configuration文件来代替Default,然后在完成之后将其销毁。

Outlook Interop库不只是用于插件。 例如,它可以用来编写一个只读取所有Outlook联系人的控制台应用程序。 我很确定,标准的Microsoft Outlook Interop库会让你阅读邮件 – 尽pipe它可能会在Outlook中抛出安全提示,用户将不得不单击。

EDITS :实际上,使用Outlook Interop实现邮件阅读取决于您对“独立”的定义。 Outlook互操作库需要在客户机上安装Outlook才能正常工作。

 // Dumps all email in Outlook to console window. // Prompts user with warning that an application is attempting to read Outlook data. using System; using System.Collections.Generic; using System.Linq; using System.Text; using Outlook = Microsoft.Office.Interop.Outlook; namespace OutlookEmail { class Program { static void Main(string[] args) { Outlook.Application app = new Outlook.Application(); Outlook.NameSpace outlookNs = app.GetNamespace("MAPI"); Outlook.MAPIFolder emailFolder = outlookNs.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); foreach (Outlook.MailItem item in emailFolder.Items) { Console.WriteLine(item.SenderEmailAddress + " " + item.Subject + "\n" + item.Body); } Console.ReadKey(); } } } 

正如你在其中一个链接的SO问题中提到的那样,我还build议使用Redemption库。 我正在使用它在一个商业应用程序处理Outlook邮件和执行各种任务与他们。 它完美地工作,并防止出现恼人的安全警报。 这将意味着使用COM Interop,但这不应该是一个问题。

这个软件包中有一个名为RDO的库,它取代了CDO 1.21,它允许你直接访问PST文件。 然后就像写(VB6代码)一样简单:

 set Session = CreateObject("Redemption.RDOSession") 'open or create a PST store set Store = Session.LogonPstStore("c:\temp\test.pst") set Inbox = Store.GetDefaultFolder(6) 'olFolderInbox MsgBox Inbox.Items.Count 

我经历了,并做了子文件夹的重构

  private static IEnumerable<MailItem> readPst(string pstFilePath, string pstName) { List<MailItem> mailItems = new List<MailItem>(); 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); string storeInfo = null; foreach (Store store in outlookNs.Stores) { storeInfo = store.DisplayName; storeInfo = store.FilePath; storeInfo = store.StoreID; } MAPIFolder rootFolder = outlookNs.Stores[pstName].GetRootFolder(); // Traverse through all folders in the PST file Folders subFolders = rootFolder.Folders; foreach (Folder folder in subFolders) { ExtractItems(mailItems, folder); } // Remove PST file from Default Profile outlookNs.RemoveStore(rootFolder); return mailItems; } private static void ExtractItems(List<MailItem> 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; mailItems.Add(mailItem); } } foreach (Folder subfolder in folder.Folders) { ExtractItems(mailItems, subfolder); } } 

您可以使用pstsdk.net:PST文件格式SDK库的.NET端口,它是开源的,可以在安装Outlook的情况下读取pst文件。

对于那些提到他们没有看到商店collections的人:

Stores集合是在Outlook 2007中添加的。因此,如果您使用的是从早期版本创build的互操作库(试图独立版本 – 这是常见的),那么这就是为什么你不会看到商店采集。

您唯一的select去商店是做以下之一:

  • 使用Outlook 2007的互操作库(这意味着您的代码将不适用于早期版本的Outlook)。
  • 使用Outlook对象模型枚举所有顶级文件夹,提取每个文件夹的StoreID,然后使用CDO或MAPI接口获取有关每个商店的更多信息。
  • 枚举CDO会话对象的InfoStores集合,然后使用InfoStore对象的字段集合来获取有关每个存储的更多信息。
  • 或者(最难的方法)使用扩展的MAPI调用(在C ++中):IMAPISession :: GetMsgStoresTable。

我们打算用这个来提供一个不依赖于outlook的解决scheme。

http://www.independentsoft.de/pst/index.html

这是非常昂贵的,但我们希望能够缩短开发时间,提高质量。

另一个可选解决scheme: NetPstExtractor

这是一个.Net API来读取没有安装Outlook的Outlook PST文件。

你可以在这里find演示版本。

MAPI API是您正在寻找的。 不幸的是它不能在.NET中使用,所以恐怕你不得不求助于非托pipe代码。

一个快速的谷歌揭示了几个包装可用,也许他们为你工作?

这也可能有帮助: http : //www.wischik.com/lu/programmer/mapi_utils.html

这个用于Outlook的.NET连接器可能会让你开始。

是的,您可以使用MS Access,然后您可以导入您的PST内容或只是链接(慢!)。

是的,使用Independentsoft PST .NET可以读取/导出受密码保护和encryption的.pst文件。

我直接从Microsoft发现了一些资源,可能有助于完成此任务。 在MSDN上search显示以下内容。

  • 如何使用Microsoft Outlook对象库通过使用Visual C#从收件箱中检索邮件
  • 如何使用Microsoft Outlook对象库通过使用Visual C#检索约会

请注意,在添加对Microsoft.Office.Interop.Outlook的引用时, 文档坚持要通过.NET选项卡而不是COM选项卡来执行此操作。