创建 Outlook 消息 (MSG) 文件
Contents
[
Hide
]
我们的迁移技巧展示了如何使用 Aspose 产品来改进您的应用程序,并摆脱对传统自动化的依赖。
本迁移技巧展示了如何使用以下方式创建 Outlook 消息 (MSG) 文件 Microsoft Office 自动化 和 Aspose.Email。代码示例在保存文件到磁盘之前设置 MSG 文件的基本属性 - 收件人、抄送、主题和 HTML 正文。
Office 自动化
要使用 Office 自动化,运行代码的机器必须安装 Microsoft Outlook。还需要引用 Outlook.interop.dll。
编程示例
以下代码片段使用 Office 自动化创建 MSG 文件。
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 互操作。使用这种方式创建 msg 文件不需要安装 Outlook。
// 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());