如何从outlook 2007 pst文件中获取电子邮件和用户信息的记录?

时间:2013-08-23 06:13:58

标签: c# outlook

我有一个代码表明没有创建即时应用程序

当我执行此代码时,此show输出:此文件“@”C:\ Users \ Toseef Abbasi \ AppData \ Local \ Microsoft \ Outlook“不是文件的有效Outlook个人文件夹< .pst> 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:\Users\Toseef Abbasi\AppData

             \Local\Microsoft\Outlook", "Outltoseefabbasi@hotmail.com-0000000b");

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

1 个答案:

答案 0 :(得分:1)

在下面的行中,您将其指向文件夹而不是实际文件。

IEnumerable<MailItem> mailItems = readPst(@"C:\Users\Toseef Abbasi\AppData

             \Local\Microsoft\Outlook", "Outltoseefabbasi@hotmail.com-0000000b");

如下所示pstname.pst是实际的pst文件。

IEnumerable<MailItem> mailItems = readPst(@"C:\Users\Toseef Abbasi\AppData

             \Local\Microsoft\Outlook\pstname.pst", "Outltoseefabbasi@hotmail.com-0000000b");