ข้อความนำ
บทความนี้อธิบายวิธีการใช้ไลบรารีวัตถุ Microsoft Outlook 2002 หรือไลบรารีวัตถุ Microsoft Office Outlook 2003 เพื่อส่งข้อความที่จัดรูปแบบเป็น HTML โดยใช้ Microsoft Visual C#
ข้อมูลเพิ่มเติม
เมื่อต้องการใช้ไลบรารีOutlook 2002 หรือไลบรารีวัตถุ Outlook 2003 เพื่อส่งข้อความที่มีการจัดรูปแบบ HTML โดยใช้ Microsoft Visual C# ให้ปฏิบัติตามขั้นตอนต่อไปนี้
-
ใน Microsoft Visual Studio .NET หรือใน Microsoft Visual Studio 2005 ให้สร้างโครงการแอปพลิเคชันคอนโซลใหม่:
-
บนเมนู ไฟล์ ให้ชี้ไปที่ใหม่ แล้วคลิก Projectไฟล์
-
ภายใต้ Projectประเภท ให้คลิก โครงการ Visual C#
หมายเหตุ ใน Microsoft Visual C# 2005 ให้คลิก Visual C#Projectชนิดอื่นๆ -
ภายใต้ เทมเพลต ให้คลิก แอปพลิเคชันคอนโซล
-
คลิก ตกลง ตามค่าเริ่มต้น ไฟล์ที่มีชื่อ Class1.cs จะถูกสร้างขึ้น
หมายเหตุ ใน Microsoft Visual C# 2005 Program.cs จะถูกสร้างขึ้นตามค่าเริ่มต้น
-
-
เพิ่มการอ้างอิงไปยังไลบรารีวัตถุ Outlook 2002 หรือไลบรารีOutlook 2003 โดยทำตามขั้นตอนต่อไปนี้:
-
บนเมนู Project ให้คลิก เพิ่มการอ้างอิง
-
คลิกแท็บ COM
-
บนแท็บ COM ให้คลิก ไลบรารีวัตถุ Microsoft Outlook 11.0 ถ้าคุณใช้ Outlook 2003 หรือคลิก ไลบรารีวัตถุ Microsoft Outlook 10.0 ถ้าคุณOutlook 2002
-
คลิก เลือก
หมายเหตุ ใน Visual C# 2005 คุณไม่าเป็น ต้องคลิก เลือก -
ในกล่องโต้ตอบ เพิ่ม การอ้างอิง ให้คลิก ตกลง
หมายเหตุ ถ้าคุณได้รับข้อความเพื่อสร้างกระดาษห่อของไลบรารี ที่คุณเลือก ไว้ ให้คลิก ใช่
-
-
ในหน้าต่างโค้ด 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;
}
}
} -
ในโค้ดนี้ ให้เปลี่ยนแปลงตามความจําเป็นที่คุณเห็นข้อคิดเห็น "TO DO"
-
กด F5 เพื่อสร้าง แล้วเรียกใช้โปรแกรม
-
ตรวจสอบว่าข้อความอีเมลถูกส่งและได้รับแล้ว
อ้างอิง
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