簡介

本文說明如何使用 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. 在 [檔案>功能表上,指向 [新增,然後按一下 [Project。

    2. [Project類型# 下,按一下[Visual C# Projects注意 在 Microsoft Visual C# 2005 中,按一下 [類型Project Visual C#

    3. [範本」下,按一下 [主控台應用程式

    4. 按一下 [確定]。 根據預設,會建立名為 Class1.cs 的檔案。注意:在 Microsoft Visual C# 2005 中,Program.cs 預設為建立。

  2. 新增 2002 物件程式庫Outlook或 2003 物件程式庫Outlook參照。 如果要執行這項操作,請依照下列步驟執行:

    1. [Project功能表上,按一下 [新增參照

    2. 按一下 [COM > Tab。

    3. 如果您使用的是 Outlook 2003,請在 [COM Outlook 11.0 物件程式庫> 上按一下 [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 開發人員網路 (MSDN) 網站:

HTTP://msdn2.microsoft.com/en-us/library/aa188489 (office.10) .aspx若要進一步Outlook 2002 電子郵件安全性功能,以及這些功能如何影響自訂解決方案,請按一下下列文章編號以在 Microsoft 知識庫中查看文章:

290500 2002 年 Outlook 中與開發人員相關的電子郵件安全性功能描述

需要更多協助嗎?

想要其他選項嗎?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。