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

标签 c# outlook outlook-2007 outlook-2003 pst

是否可以使用 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 已安装并已为当前用户配置。它使用默认配置文件(您可以通过转到控制面板中的邮件来编辑默认配置文件)。此代码的一项主要改进是创建一个临时配置文件来代替默认配置文件,然后在完成后将其销毁。

最佳答案

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

关于c# - 我可以在 C# 中读取 Outlook (2003/2007) PST 文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/577904/

相关文章:

c# - C# 中的 foreach 和带有 List<T> 的传统 for 循环

c# - 如何使用 C# 从 JSON 数组中获取特定值

c# - 如何将元素的背景颜色与 2 个半透明元素背景颜色相匹配

c# - 找不到类型或命名空间名称 'Column'

java - iCal 中的 HTML 使用 iCal4j

vba - 将邮件格式更改为 html 时如何获取 UI 行为?

excel - 如何在同一屏幕上显示打印对话框和打印预览?

outlook - 在不同时区使用 Outlook 365 API 设置全天日历事件

vsto - 在 Outlook 中使用 vsto,如何触发新邮件 toast /警报?

outlook - 如何以编程方式判断安装了哪些 Outlook 插件以及它们是否已启用?