Microsoft Outlook オブジェクト ライブラリを使用して、受信トレイから Visual C# を使用してメッセージを取得する方法

概要

この記事では、Microsoft Outlook 2002 オブジェクト ライブラリまたは Microsoft Office Outlook 2003 オブジェクト ライブラリを使用して、Microsoft Visual C# を使用して受信トレイからメッセージを取得する方法について説明します。

詳細

Outlook 2002 オブジェクト ライブラリまたは Outlook 2003 オブジェクト ライブラリを使用して、Visual C# を使用して受信トレイからメッセージを取得するには、次の手順に従います。

  1. Microsoft Visual Studio .NET または Visual Studio 2005 で、新しいコンソール アプリケーション プロジェクトを作成します。

    1. [ファイル] メニューの [新規作成] をポイントし、[プロジェクト] を選択します。

    2. [ プロジェクトの種類] で、[ Visual C# プロジェクト] を選択します。

      注:

      Visual Studio 2005 で、[ Visual C#] を選択します。

    3. [ テンプレート] で、[ コンソール アプリケーション] を選択します。

    4. [OK] を選択します。 既定では、Class1.cs という名前 ファイルが作成されます。

      注:

      Visual Studio 2005 では、Program.csは既定で作成されます。

  2. Outlook 2002 オブジェクト ライブラリまたは Outlook 2003 オブジェクト ライブラリへの参照を追加します。 これを行うには、次の手順を実行します。

    1. [ プロジェクト ] メニューの [ 参照の追加] を選択します。

    2. [ COM ] タブを選択します。

    3. [ COM ] タブで、 Outlook 2003 を使用している場合は [Microsoft Outlook 11.0 オブジェクト ライブラリ ] を選択し、Outlook 2002 を使用している場合は [Microsoft Outlook 10.0 オブジェクト ライブラリ ] を選択します。

    4. [ 選択] を選択します

    5. [ 参照の追加 ] ダイアログ ボックスで、[ OK] を選択します

      注:

      選択したライブラリのラッパーを生成するメッセージが表示された場合は、[ はい] を選択します。

  3. [Class1.cs コード] ウィンドウで、既存のすべてのコードを次のコードに置き換えます。

    using System;
    using System.Reflection; // to use Missing.Value
    
    //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.
    //using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace ConsoleApplication1
    {
    public class Class1
    {
    public static int Main(string[]args)
    {
    try
    {
    // Create the Outlook application.
    // in-line initialization
    Outlook.Application oApp = new Outlook.Application();
    
    // Get the MAPI namespace.
    Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
    // Log on by using the default profile or existing session (no dialog box).
    oNS.Logon(Missing.Value,Missing.Value,false,true);
    
    // Alternate logon method that uses a specific profile name.
    // TODO: If you use this logon method, specify the correct profile name
    // and comment the previous Logon line.
    //oNS.Logon("profilename",Missing.Value,false,true);
    
    //Get the Inbox folder.
    Outlook.MAPIFolder oInbox = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    
    //Get the Items collection in the Inbox folder.
    Outlook.Items oItems = oInbox.Items;
    
    // Get the first message.
    // Because the Items folder may contain different item types,
    // use explicit typecasting with the assignment.
    Outlook.MailItem oMsg = (Outlook.MailItem)oItems.GetFirst();
    
    //Output some common properties.
    Console.WriteLine(oMsg.Subject);
    Console.WriteLine(oMsg.SenderName);
    Console.WriteLine(oMsg.ReceivedTime);
    Console.WriteLine(oMsg.Body);
    
    //Check for attachments.
    int AttachCnt = oMsg.Attachments.Count;
    Console.WriteLine("Attachments: " + AttachCnt.ToString());
    
    //TO DO: If you use the Microsoft Outlook 10.0 Object Library, uncomment the following lines.
    /*if (AttachCnt > 0) 
    {
    for (int i = 1; i <= AttachCnt; i++) 
     Console.WriteLine(i.ToString() + "-" + oMsg.Attachments.Item(i).DisplayName);
    }*/
    
    //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following lines.
    /*if (AttachCnt > 0) 
    {
    for (int i = 1; i <= AttachCnt; i++) 
     Console.WriteLine(i.ToString() + "-" + oMsg.Attachments[i].DisplayName);
    }*/
    
    //Display the message.
    oMsg.Display(true); //modal
    
    //Log off.
    oNS.Logoff();
    
    //Explicitly release objects.
    oMsg = null;
    oItems = null;
    oInbox = null;
    oNS = null;
    oApp = null;
    }
    
    //Error handler.
    catch (Exception e)
    {
    Console.WriteLine("{0} Exception caught: ", e);
    }
    
    // Return value.
    return 0;
    
    }
    }
    }
    
  4. このコードでは、"TO DO" コメントが表示される場所で必要な変更を行います。

  5. F5 キーを押してビルドし、プログラムを実行します。

関連情報

詳細については、「 Microsoft Office Development with Visual Studio」を参照してください。