ข้อความนำ

บทความนี้อธิบายวิธีการใช้ไลบรารีวัตถุ Microsoft Outlook 2002 หรือไลบรารีวัตถุ Microsoft Office Outlook 2003 เพื่อส่งข้อความที่จัดรูปแบบเป็น HTML โดยใช้ Microsoft Visual C#

ข้อมูลเพิ่มเติม

เมื่อต้องการใช้ไลบรารีOutlook 2002 หรือไลบรารีวัตถุ Outlook 2003 เพื่อส่งข้อความที่มีการจัดรูปแบบ HTML โดยใช้ Microsoft Visual C# ให้ปฏิบัติตามขั้นตอนต่อไปนี้

  1. ใน Microsoft Visual Studio .NET หรือใน Microsoft Visual Studio 2005 ให้สร้างโครงการแอปพลิเคชันคอนโซลใหม่:

    1. บนเมนู ไฟล์ ให้ชี้ไปที่ใหม่ แล้วคลิก Projectไฟล์

    2. ภายใต้ Projectประเภท ให้คลิก โครงการ Visual C#หมายเหตุ ใน Microsoft Visual C# 2005 ให้คลิก Visual C#Projectชนิดอื่นๆ

    3. ภายใต้ เทมเพลต ให้คลิก แอปพลิเคชันคอนโซล

    4. คลิก ตกลง ตามค่าเริ่มต้น ไฟล์ที่มีชื่อ Class1.cs จะถูกสร้างขึ้นหมายเหตุ ใน Microsoft Visual C# 2005 Program.cs จะถูกสร้างขึ้นตามค่าเริ่มต้น

  2. เพิ่มการอ้างอิงไปยังไลบรารีวัตถุ Outlook 2002 หรือไลบรารีOutlook 2003 โดยทำตามขั้นตอนต่อไปนี้:

    1. บนเมนู Project ให้คลิก เพิ่มการอ้างอิง

    2. คลิกแท็บ COM

    3. บนแท็บ COM ให้คลิก ไลบรารีวัตถุ Microsoft Outlook 11.0 ถ้าคุณใช้ Outlook 2003 หรือคลิก ไลบรารีวัตถุ Microsoft Outlook 10.0 ถ้าคุณOutlook 2002

    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. ตรวจสอบว่าข้อความอีเมลถูกส่งและได้รับแล้ว

อ้างอิง

For more information, visit the following Microsoft Developer Network (MSDN):

http://msdn2.microsoft.com/en/library/aa188489(office.10).aspxFor more information about the Outlook 2002 e-mail security features and about how those features can affect custom solutions, click the following article number to view the article in the Microsoft Knowledge Base:

290500 รายละเอียดเกี่ยวกับฟีเจอร์ความปลอดภัยของอีเมลที่เกี่ยวข้องกับนักพัฒนาใน Outlook 2002

ต้องการความช่วยเหลือเพิ่มเติมหรือไม่

ต้องการตัวเลือกเพิ่มเติมหรือไม่

สํารวจสิทธิประโยชน์ของการสมัครใช้งาน เรียกดูหลักสูตรการฝึกอบรม เรียนรู้วิธีการรักษาความปลอดภัยอุปกรณ์ของคุณ และอื่นๆ