在非默认Outlook 2007帐户的C#中创建MAPIFolder对象

时间:2016-12-30 17:47:03

标签: c# email outlook-2007

我正在Visual Studio 2015中创建一个C#控制台应用程序,它将所有电子邮件打印到控制台。我尝试创建MAPIFolder对象时遇到问题。我使用了这篇文章中的代码:Error: "Could Not Find Installable ISAM"。我可以使用命名空间从默认帐户创建MAPIFolder对象,但我无法使用商店创建任何文件夹对象。

using Microsoft.Office.Interop.Outlook;
using static System.Console;

namespace MoveEmailsDriver

{
    class ProcessEmails
    {
        static void Main(string[] args)
        {
                PrintEmailBody();
        }
        public static void PrintEmailBody()
        {
            Application app = new Application();
            _NameSpace ns = app.GetNamespace("MAPI");
            Stores stores = ns.Stores;

            foreach(Store store in stores)
            {
                MAPIFolder inboxFolder = store.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

                foreach(MailItem item in inboxFolder.Items)
                {
                    WriteLine(item.Body);
                }
            }
        }

    }
}

Read emails from non default accounts in Outlook

1 个答案:

答案 0 :(得分:0)

我明白了。我不得不使用GetRootFolder()方法创建一个MAPIFolder对象。这是更新的代码:

using Microsoft.Office.Interop.Outlook;
using static System.Console;

namespace OutlookDriverProgram
{
    class Program
    {
        static void Main(string[] args)
        {
            Application app = new Application();
            NameSpace ns = app.GetNamespace("MAPI");
            Stores stores = ns.Stores;

            foreach (Store store in stores)
            {
                //Uncomment next line to see the folder names
                //WriteLine("Folder name = {0}", store.DisplayName);
                if (store.DisplayName.Equals("YOURFOLDERNAME"))
                {

                    MAPIFolder YOURFOLDERNAME = store.GetRootFolder();

                    foreach (Folder subF in YOURFOLDERNAME.Folders)
                    {

                        if (subF.Name.Equals("Inbox"))
                        {
                            foreach (MailItem email in subF.Items)
                            {
                                WriteLine("Email subject = {0}", email.Subject);
                            }

                        }

                    }
                }

            }
        }

    }
}