可以使用C#读取.PST文件吗?我想把它作为一个独立的应用程序,而不是Outlook插件(如果可能的话)。
如果我见过other SO questions similar提到MailNavigator,但我希望在C#中以编程方式做到这一点。
我看过Microsoft.Office.Interop.Outlook名称空间,但这似乎只适用于Outlook外接程序。LibPST似乎能够读取PST文件,但这是在C(对不起乔尔,我没有learn C before graduating)。
如有任何帮助,将不胜感激,谢谢!
编辑:
感谢大家的回复!我接受了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。它使用默认配置文件(您可以通过转到控制面板中的邮件来编辑默认配置文件)。这段代码的一个主要改进是创建一个临时配置文件来代替默认配置文件,然后在完成后销毁它。
发布于 2009-02-23 15:12:15
Outlook Interop库不仅仅适用于插件。例如,它可以用来编写一个只读取所有Outlook联系人的控制台应用程序。我非常确定,标准的Microsoft Outlook Interop库将允许您阅读邮件-尽管它可能会在Outlook中抛出一个安全提示,用户必须单击该提示。
编辑:使用Outlook Interop实际实现邮件阅读取决于您对“独立”的定义。Outlook Interop库要求在客户端计算机上安装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();
}
}
}
发布于 2009-02-23 19:12:37
正如您在其中一个链接的SO问题中所提到的,我也推荐使用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
发布于 2009-11-29 05:24:40
我完成了子文件夹的重构。
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);
}
}
https://stackoverflow.com/questions/577904
复制相似问题