小编典典

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

c#

是否可以使用C#读取.PST文件?我想作为一个独立的应用程序,而不是作为Outlook加载项(如果可能)来执行此操作。

如果看到其他 类似于此的SO
问题
,请提及MailNavigator,但我希望以C#方式进行编程。

我查看了Microsoft.Office.Interop.Outlook命名空间,但这似乎仅用于Outlook加载项。LibPST似乎能够读取PST文件,但这是用C语言编写的(对不起,乔尔,我毕业之前没有学习C语言)。

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

编辑:

谢谢大家的答复!我接受了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,并且已经为当前用户配置了Outlook。它使用默认配置文件(您可以通过控制面板中的“邮件”来编辑默认配置文件)。此代码的一个主要改进是创建一个临时配置文件以代替Default使用,然后在完成后销毁它。


阅读 440

收藏
2020-05-19

共1个答案

小编典典

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();
    }
}
}
2020-05-19