我可以读的Outlook(2003/2007)PST在C#中的文件吗? [英] Can I read an Outlook (2003/2007) PST file in C#?

查看:484
本文介绍了我可以读的Outlook(2003/2007)PST在C#中的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to read a .PST file using C#? I would like to do this as a standalone application, not as an Outlook addin (if that is possible).

如果看到其他 <一个href=\"http://stackoverflow.com/questions/486883/problem-in-releasing-memory-from-an-outlook-pst-file\">SO <一href=\"http://stackoverflow.com/questions/235231/how-to-avoid-outlook-security-alert-when-reading-outlook-message-from-c-program\">questions 类似以这种提的 MailNavigator 但我期待在C#编程方式做到这一点。

If have seen other SO questions similar to this mention MailNavigator but I am looking to do this programmatically in C#.

我已经看了<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.aspx\">Microsoft.Office.Interop.Outlook命名空间,但似乎是只为Outlook加载项。 LibPST 似乎能够读取PST文件,但是这是在C(对不起乔尔,我没有<一个HREF =htt​​p://www.joelonsoftware.com/articles/CollegeAdvice.html>毕业)前学习C

I have looked at the Microsoft.Office.Interop.Outlook namespace but that appears to be just for Outlook addins. LibPST appears to be able to read PST files, but this is in C (sorry Joel, I didn't learn C before graduating).

任何帮助将大大AP preciated,谢谢!

Any help would be greatly appreciated, thanks!

编辑:

感谢大家的答复!我接受马修拉斯顿作为答案响应,因为它最终导致我的code我一直在寻找。下面是我得到了什么工作(你将需要添加到Microsoft.Office.Interop.Outlook的引用)一个简单的例子:

Thank you all for the responses! I accepted Matthew Ruston's response as the answer because it ultimately led me to the code I was looking for. Here is a simple example of what I got to work (You will need to add a reference to 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;
        }
    }
}

注::此code假定的Outlook安装并已配置为当前用户。它使用默认配置(您可以进入控制面板到邮件编辑默认的配置文件)。这个code的一个主要改进是创建一个临时配置文件,而不是使用的默认值,然后摧毁它一次完成。

Note: This code assumes that Outlook is installed and already configured for the current user. It uses the Default Profile (you can edit the default profile by going to Mail in the Control Panel). One major improvement on this code would be to create a temporary profile to use instead of the Default, then destroy it once completed.

推荐答案

在Outlook互操作库不只是加载项。例如,它可以用来写一个控制台应用程序,只是读取您的所有Outlook联系人。我是pretty确保标准的Microsoft Outlook互操作库将让你阅读邮件 - 尽管它可能会在Outlook中抛出一个安全提示,用户必须通过点击

The Outlook Interop library is not just for addins. For example it could be used to write a console app that just reads all your Outlook Contacts. I am pretty sure that the standard Microsoft Outlook Interop library will let you read the mail - albeit it will probably throw a security prompt in Outlook that the user will have to click through.

EDITS :实际执行使用Outlook互操作的邮件阅读取决于你的独立的定义是指。在Outlook互操作的lib需要Outlook客户端机器上,以功能进行安装。

EDITS: Actually implementing mail reading using Outlook Interop depends on what your definition of 'standalone' means. The Outlook Interop lib requires Outlook to be installed on the client machine in order to function.

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

这篇关于我可以读的Outlook(2003/2007)PST在C#中的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆