2016-12-30 6 views
0

Visual Studio 2015ですべての電子メールをコンソールに出力するC#コンソールアプリケーションを作成しています。私はMAPIFolderオブジェクトを作成しようとすると問題が発生します。私はこのポストのコードを使用した:Read emails from non default accounts in Outlook。名前空間を使用して既定のアカウントからMAPIFolderオブジェクトを作成できますが、ストアを使用してフォルダオブジェクトを作成することはできません。デフォルト以外のOutlook 2007アカウントでC#で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); 
       } 
      } 
     } 

    } 
} 

This is the exception error I am getting.

答えて

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

         } 

        } 
       } 

      } 
     } 

    } 
} 
関連する問題