向 Outlook MSG 文件添加附件和 HTML 文本
Contents
[
Hide
]
我们的迁移技巧展示了如何使用 Aspose 产品来改进您的应用程序,并摆脱对传统自动化的依赖。
本迁移技巧展示了如何创建带有 HTML 格式正文并添加多个附件的 MSG 文件:
- 使用以下内容的 VBA 代码片段 Microsoft Office 自动化 来创建带有附件和 HTML 正文的 MSG 文件。
- 使用以下方式实现相同的功能 Aspose.Email for Java.
Office 自动化
使用此方法时,VBA 代码运行的机器必须安装 Microsoft Outlook。下面的代码片段创建了一个带有附件和 HTML 正文的 Outlook MSG 文件。
VBA
' Create an object of type Outlook.Application
Set objOutlookApplication = CreateObject("Outlook.Application")
' Create an object of type olMailItem
Set objMsg = objOutlookApplication.CreateItem(olMailItem)
' Set properties of the message file e.g. subject, body and to address
' Set subject
objMsg.Subject = "This MSG file is created using Office Automation."
' Set to (recipient) address
objMsg.To = "to@domain.com"
' Set body of the email message
objMsg.HTMLBody = "<html><p>This MSG file is created using VBA code.</p>"
' Add attachments to the message
objMsg.Attachments.Add "C:\test.bmp"
objMsg.Attachments.Add "C:\test2.jpg"
' Save as Outlook MSG file
objMsg.SaveAs ("c:\testvba.msg")
' Open the MSG file
objMsg.Display
Aspose.Email for Java
下面的代码片段使用 Aspose.Email for Java 库创建 MSG 文件,类似于 上述创建的文件,包含多个附件和 HTML 正文。由于 Aspose.Email for Java 完全使用 Java 编写,无需 COM 互操作。此外,机器上不必安装 Microsoft Outlook 2003/2007。下面描述的方法适用于未安装 Microsoft Outlook 或希望在服务器上生成 MSG 文件的情况。
下面的代码片段展示了如何使用 Aspose.Email for Java 在 Java 中完成相同的任务:
// Create an instance of type MailMessage
MailMessage msg = new MailMessage();
// Set properties of message like subject, to and HTML body
// Set subject
msg.setSubject("This MSG file is created using Aspose.Email for .NET");
// Set from (sender) address
msg.setSender(new MailAddress("from@domain.com", "From Name"));
// Set to (recipient) address and name
msg.getTo().addItem(new MailAddress("to@domain.com", "To Name"));
// Set HTML body of the email message
msg.setHtmlBody("<html><p>This MSG file is created using Java code.</p>"
+ "<p>Microsoft Outlook does not need to be installed on the machine running this code.</p>"
+ "<p>This method is suitable for creating MSG files on the server side.</html>");
// Add attachments to the message file
msg.getAttachments().addItem(new Attachment("C:\\test.bmp"));
msg.getAttachments().addItem(new Attachment("C:\\test2.jpg"));
// Save as Outlook MSG file
String strSaveFile = "C:\\TestAspose.msg";
msg.save(strSaveFile, SaveOptions.getDefaultMsgUnicode());