การสร้างไฟล์ข้อความ Outlook (MSG)

Office Automation

เพื่อใช้ Office Automation จำเป็นต้องติดตั้ง Microsoft Outlook บนเครื่องที่รหัสทำงาน และต้องอ้างอิงไฟล์ Outlook.interop.dll ด้วย

ตัวอย่างการโปรแกรม

โค้ดส่วนต่อไปนี้สร้างไฟล์ MSG โดยใช้ Office Automation.

C#


 // Creates a new Outlook Application instance

Outlook.Application objOutlook = new Outlook.Application();

// Creating a new Outlook message from the Outlook Application instance

Outlook.MailItem msgInterop = (Outlook.MailItem)(objOutlook.CreateItem(Outlook.OlItemType.olMailItem));

// Set recipient information

msgInterop.To = "to@domain.com";

msgInterop.CC = "cc@domain.com";

// Set the message subject

msgInterop.Subject = "Subject";

// Set some HTML text in the HTML body

msgInterop.HTMLBody = "<h3>HTML Heading 3</h3> <u>This is underlined text</u>";

// Save the MSG file in local disk

string strMsg = @"c:\\temp\TestInterop.msg";

msgInterop.SaveAs(strMsg, Outlook.OlSaveAsType.olMSG);

Aspose.Email for Java

ตัวอย่างด้านล่างใช้ Aspose.Email เพื่อสร้างไฟล์ Outlook MSG เขียนด้วย Java แท้ ๆ ไม่ใช้ COM Interop ไม่จำเป็นต้องติดตั้ง Outlook เพื่อสร้างไฟล์ msg แบบนี้


// Create an instance of the Aspose.Email.MailMessage class
MailMessage msg = new MailMessage();

// Set recipients information
msg.setTo(MailAddressCollection.to_MailAddressCollection("to@domain.com"));
msg.setCC(MailAddressCollection.to_MailAddressCollection("cc@domain.com"));

// Set the subject
msg.setSubject("Subject");

// Set HTML body
msg.setHtmlBody("<h3>HTML Heading 3</h3> <u>This is underlined text</u>");

// Add an attachment
msg.getAttachments().addItem(new Attachment("test.txt"));

// Save it in local disk
String strMsg = "c:\\ TestAspose.msg";
msg.save(strMsg, SaveOptions.getDefaultMsgUnicode());