创建和保存 MSG 文件

Aspose.Email 支持创建 Outlook 消息(MSG)文件。本文章解释了如何:

  • 创建 MSG 消息。
  • 创建带附件的 MSG 消息。
  • 使用 RTF 正文创建 MSG 消息。
  • 将消息保存为草稿。
  • 使用正文压缩。

创建和保存 Outlook 消息

MailMessage 类具有 保存 可以将 Outlook MSG 文件保存到磁盘或流的 method。下面的代码片段创建了该类的实例。 MailMessage 类,设置如 from、to、subject 和 body 等属性。该 保存 方法将文件名作为参数。此外,还可以使用 … 创建 Outlook 消息。 压缩的 RTF 正文 使用 MapiConversionOptions.

  1. 创建该类的新实例 MailMessage 类,并设置 From、To、Subject 和 Body 属性。
  2. 调用 MapiMessage 类 fromMailMessage 方法接受 … 类的对象 MailMessage 类型。该 fromMailMessage 方法将 … 转换为 MailMessage 转换为 a MapiMessage (MSG)。
  3. 调用 MapiMessage.save 方法来保存 MSG 文件。
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);

// Save the message (MSG) file
String strMsgFile = "CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.save(dataDir + strMsgFile);

使用附件创建 MSG 文件

在上面的示例中, 我们创建了一个简单的 MSG 文件。Aspose.Email 也支持保存带附件的消息文件。您所需做的只是将附件添加到 … MailMessage 实例。通过在 … 上调用 addItem 方法来添加附件。 MailMessage.Attachments 集合。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

String[] files = new String[2];
files[0] = "attachment.doc";
files[1] = "attachment.png";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setBody("This is test body");

// Add the attachments
for (String strFileName : files)
{
    mailMsg.getAttachments().addItem(new Attachment(strFileName));
}

// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
String strMsgFile = "CreateMessagesWithAttachments.msg";
outlookMsg.save(dataDir + strMsgFile);

使用 RTF 正文创建 MSG 文件

您还可以使用 Aspose.Email 创建带有富文本(RTF)正文的 Outlook 消息(MSG)文件。RTF 正文支持文本格式化。通过设置 … 来创建。 MailMessage.HtmlBody 属性。当您转换 a MailMessage 实例转换为 a MapiMessage 实例中,HTML 正文被转换为 RTF。这样,电子邮件正文的格式得以保留。

下面的示例创建了一个带有 RTF 正文的 MSG 文件。HTML 正文中包含一个标题、加粗和下划线格式。转换为 RTF 时,这些格式会被保留。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();

// Set from, to, subject and body properties
mailMsg.setFrom(MailAddress.to_MailAddress("sender@domain.com"));
mailMsg.setTo(MailAddressCollection.to_MailAddressCollection("receiver@domain.com"));
mailMsg.setSubject("This is test message");
mailMsg.setHtmlBody("<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");

MapiMessage outlookMsg = MapiMessage.fromMailMessage(mailMsg);
outlookMsg.save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");

以草稿状态保存消息

当用户已开始编辑邮件但想稍后返回完成时,邮件会以草稿形式保存。Aspose.Email 支持通过设置消息标志将电子邮件保存为草稿状态。以下示例代码演示了如何将 Outlook 邮件(MSG)保存为草稿。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = "outlook/";

// Change properties of an existing MSG file
String strExistingMsg = "message.msg";

// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.setSubject(msg.getSubject() + " NEW SUBJECT (updated by Aspose.Email)");
msg.setHtmlBody(msg.getHtmlBody() + " NEW BODY (udpated by Aspose.Email)");

// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.fromMailMessage(msg);
mapiMsg.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.save(dataDir + "SavingMessageInDraftStatus_out.msg");

正文压缩的影响

RTF 正文压缩方法可用于生成更小的 MSG 文件,但会导致创建速度变慢。若要提高创建速度,请将该标志设为 false。该标志还会影响生成的 PST:更小的 MSG 文件会产生更小的 PST,而大型 MSG 文件会导致 PST 创建变慢。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String fileName = "outlook/test.msg";

MailMessage message = MailMessage.load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.setUseBodyCompression(true);
MapiMessage ae_mapi = MapiMessage.fromMailMessage(message, options);