如何使用 Microsoft Outlook 物件函式庫,透過 Visual C 傳送 HTML 格式的訊息#

簡介

本文說明如何使用 Microsoft Outlook 2002 物件函式庫或 Microsoft Office Outlook 2003 物件函式庫,透過 Microsoft Visual C# 傳送 HTML 格式的訊息。

更多資訊

若要使用 Outlook 2002 物件函式庫或 Outlook 2003 物件函式庫,使用 Microsoft Visual C# 傳送 HTML 格式的訊息,請遵循以下步驟:

  1. 在 Microsoft Visual Studio .NET 或 Microsoft Visual Studio 2005 中,建立一個新的控制台應用程式專案:

    1. [檔案] 功能表中,按一下 [新增],然後按一下 [專案]

    2. 專案類型中,點選 Visual C# 專案

      注意:在 Microsoft Visual C# 2005 中,點擊專案類型中的 Visual C#

    3. 範本中,點選 主控台應用程式

    4. 按一下 [確定]。 預設情況下,會建立一個名為 Class1.cs 的檔案。

      注意Microsoft Visual C# 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. 點擊 選擇

      注意在 Visual C# 2005 中,你不必點擊 選擇

    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 SendHTMLMail
    {
       public class Class1
       {
          public static int Main(string[] args)
          {
             try
             {
                // Create the Outlook application.
                Outlook.Application  oApp = new Outlook.Application();
    
                // Get the NameSpace and Logon information.
                Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
                // Log on by using a dialog box to choose the profile.
                oNS.Logon(Missing.Value, Missing.Value, true, true); 
    
                // Alternate logon method that uses a specific profile.
                // TODO: If you use this logon method, 
                //  change the profile name to an appropriate value.
                //oNS.Logon("YourValidProfile", Missing.Value, false, true); 
    
                // Create a new mail item.
                Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
    
                // Set the subject.
                oMsg.Subject = "Send Using OOM in C#";
    
                // Set HTMLBody.
                String sHtml;
                sHtml = "<HTML>\n" + 
                   "<HEAD>\n" +
                   "<TITLE>Sample GIF</TITLE>\n" +
                   "</HEAD>\n" +
                   "<BODY><P>\n" + 
                   "<h1><Font Color=Green>Inline graphics</Font></h1></P>\n" +
                   "</BODY>\n" + 
                   "</HTML>";
                oMsg.HTMLBody = sHtml;
    
                // Add a recipient.
                Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
                // TODO: Change the recipient in the next line if necessary.
                Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("email address");
                oRecip.Resolve();
    
                // Send.
                oMsg.Send();
    
                // Log off.
                oNS.Logoff();
    
                // Clean up.
                oRecip = null;
                oRecips = null;
                oMsg = null;
                oNS = null;
                oApp = null;
             }
    
             // Simple error handling.
             catch (Exception e)
             {
                Console.WriteLine("{0} Exception caught.", e);
             }  
    
             // Default return value.
             return 0;
    
          }
       }
    }
    
  4. 在這段程式碼中,對你看到「TO DO」註解的地方做必要的修改。

  5. 按下 F5 以建立並執行程式。

  6. 確認電子郵件已發送且已被接收。

參考資料

如需更多資訊,請造訪下列 Microsoft Developer Network (MSDN) 網站:

http://msdn2.microsoft.com/en-us/library/aa188489 (辦公室.10) .aspx 欲了解更多關於 Outlook 2002 電子郵件安全功能及其如何影響自訂解決方案的資訊,請點擊以下文章編號以瀏覽 Microsoft 知識庫中的文章:

290500 Outlook 2002 中開發者相關電子郵件安全功能的說明