共用方式為


如何使用 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. 選擇 [確定]。 根據預設,會建立名為 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. 在 [ 新增參考] 對話框中,選取 [ 確定]。

      注意

      如果您收到訊息要求生成您所選取的函式庫封裝器,請選取 [ ]。

  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 建置,然後執行程式。

參考資料

如需詳細資訊,請參閱 使用 Visual Studio 的 Microsoft Office 開發