使用 Aspose.Email.Outlook 管理消息文件

将 MSG 转换为 MIME 消息

Aspose.Email API 提供使用以下方式将 MSG 文件转换为 MIME 消息的功能: toMailMessage 方法。

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage msg = new MapiMessage(
                            "sender@test.com",
                            "recipient1@test.com; recipient2@test.com",
                            "Test Subject",
                            "This is a body of message.");
MailConversionOptions options = new MailConversionOptions();
options.setConvertAsTnef(true);
MailMessage mail = msg.toMailMessage(options);

将 MSG 转换为 EML 时保留 RTF 正文

API 提供以下方法,以在将 MSG 转换为 EML 时保留 RTF 正文:

以下代码示例演示如何在 MailMessage 中保留 RTF 正文:

MsgLoadOptions options = new MsgLoadOptions();
options.setPreserveRtfContent(true);
MailMessage message = MailMessage.load("fileName", options);
MapiMessage mapi = MapiMessage.load("fileName");
MailConversionOptions options = new MailConversionOptions();
options.setPreserveRtfContent(true);
MailMessage message = mapi.toMailMessage(options);

将 MSG 转换为 MHTML 时保留类别头

Aspose.Email API 在将邮件转换为 MHTML 时提供添加类别头的功能。此功能由以下指定: MhtSaveOptions 类作为将 MailMessage 保存为 MHTML 格式时的附加选项。

以下代码示例演示如何从 MapiMessage 对象创建 MHT(MHTML)文件,使用 MhtSaveOptions 自定义 MHT 文件的格式和头部,为电子邮件设置类别,然后在保存之前修改 MHT 文件的格式模板和渲染头部。

 MapiMessage msg = new MapiMessage("from@aaa.com", "to@aaa.com", "subj", "body");

msg.setCategories(new String[] { "Urgently", "Important" });

MhtSaveOptions saveOptions = new MhtSaveOptions();

saveOptions.getFormatTemplates().set_Item(MhtTemplateName.CATEGORIES,

    saveOptions.getFormatTemplates().get_Item(MhtTemplateName.CATEGORIES).replace("Categories", "Les catégories"));

saveOptions.getRenderingHeaders().add(MhtTemplateName.CATEGORIES);

msg.save("fileName.mhtml", saveOptions);

读取和写入 Outlook 模板文件 (.OFT)

Outlook 模板在需要反复发送相似电子邮件时非常有用。与每次从头准备邮件不同,您可以先在 Outlook 中准备邮件并保存为 Outlook 模板 (OFT)。之后每次需要发送时,只需从模板创建,省去在正文或主题行中编写相同文字、设置格式等时间。Aspose.Email MailMessage 类可用于加载和读取 Outlook 模板(OFT)文件。一旦在实例中加载了 Outlook 模板, MailMessage 类,您可以更新发件人、收件人、正文、主题及其他属性。更新属性后:

  • 使用以下方式发送电子邮件 SmtpClient 类或
  • 将邮件保存为 MSG 并使用 Microsoft Outlook 进行进一步的更新/验证。

在下面的代码示例中,我们:

  1. 使用以下方式加载模板 MailMessage 类。
  2. 更新部分属性。
  3. 以 MSG 格式保存邮件。

以下代码片段展示了如何加载 OFT 文件、更新邮件并以 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/";

// Load the Outlook template (OFT) file in MailMessage's instance
MailMessage message = MailMessage.load(dataDir + "sample.oft", new MsgLoadOptions());

// Set the sender and recipients information
String senderDisplayName = "John";
String senderEmailAddress = "john@abc.com";
String recipientDisplayName = "William";
String recipientEmailAddress = "william@xzy.com";

message.setSender(new MailAddress(senderEmailAddress, senderDisplayName));
message.getTo().addMailAddress(new MailAddress(recipientEmailAddress, recipientDisplayName));
message.setHtmlBody(message.getHtmlBody().replace("DisplayName", "<b>" + recipientDisplayName + "</b>"));

// Set the name, location and time in email body
String meetingLocation = "<u>" + "Hall 1, Convention Center, New York, USA" + "</u>";
String meetingTime = "<u>" + "Monday, June 28, 2010" + "</u>";
message.setHtmlBody(message.getHtmlBody().replace("MeetingPlace", meetingLocation));
message.setHtmlBody(message.getHtmlBody().replace("MeetingTime", meetingTime));

// Save the message in MSG format and open in Office Outlook
MapiMessage mapimessage = MapiMessage.fromMailMessage(message);
mapimessage.setMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapimessage.save(dataDir + "ReadAndWritingOutlookTemplateFile_out.msg");

将 Outlook MSG 文件保存为模板

以下代码片段展示了如何将 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/";

try (MapiMessage mapi = new MapiMessage("test@from.to", "test@to.to", "template subject", "Template body")) {
    mapi.saveAsTemplate(dataDir + "mapiToOft.msg");
}

为 Outlook MSG 文件设置颜色类别

颜色类别用于标记电子邮件的某种重要性或类别。Microsoft Outlook 允许用户分配颜色类别以区分邮件。要处理颜色类别,请使用 FollowUpManager。它包含以下函数: addCategory, removeCategory, clearCategories 和 getCategories.

以下示例执行了如下任务:

  1. 添加颜色类别。
  2. 添加另一个颜色类别。
  3. 检索所有类别的列表。
  4. 移除所有类别。
// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");

// Add Two category
FollowUpManager.addCategory(msg, "Purple Category");
FollowUpManager.addCategory(msg, "Red Category");

// Retrieve the list of available categories
IList categories = FollowUpManager.getCategories(msg);

// Remove the specified category and then Clear all categories
FollowUpManager.removeCategory(msg, "Red Category");
FollowUpManager.clearCategories(msg);

从 MSG 文件访问后续信息

Aspose.Email API 提供了从已发送或已接收的邮件中获取后续信息的功能。它可以从邮件文件中检索已阅读、投递阅读回执和投票结果信息。

检索已阅读和投递回执信息

以下代码片段展示了如何检索已阅读和投递回执信息。

// 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/";

MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");
for (MapiRecipient recipient : msg.getRecipients()) {
    System.out.println("Recipient: " + recipient.getDisplayName());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY property
    System.out.println("Delivery time: " + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_DELIVERY).getDateTime());

    // Get the PR_RECIPIENT_TRACKSTATUS_TIME_READ property
    System.out.println("Read time" + recipient.getProperties().get_Item(MapiPropertyTag.PR_RECIPIENT_TRACKSTATUS_TIME_READ).getDateTime());
}

创建转发和回复消息

Aspose.Email API 提供了创建和格式化转发及回复消息的功能。该 ReplyMessageBuilderForwardMessageBuilder API 的类分别用于创建回复和转发消息。回复或转发消息可以使用以下任意模式创建: OriginalMessageAdditionMode 枚举。此枚举具有以下值:

  • OriginalMessageAdditionMode.None - 原始消息不包含在回复消息中。
  • OriginalMessageAdditionMode.Attachment - 原始消息作为附件包含在回复消息中
  • OriginalMessageAdditionMode.Textpart - 原始消息以文本形式包含在回复消息的正文中

创建回复消息

以下代码片段展示了如何创建回复消息。

// 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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ReplyMessageBuilder builder = new ReplyMessageBuilder();

// Set ReplyMessageBuilder Properties
builder.setReplyAll(true);
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
builder.setResponseText(
        "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>");

MapiMessage replyMsg = builder.buildResponse(originalMsg);
replyMsg.save(dataDir + "reply_out.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/";

MapiMessage originalMsg = MapiMessage.fromFile(dataDir + "message1.msg");
ForwardMessageBuilder builder = new ForwardMessageBuilder();
builder.setAdditionMode(OriginalMessageAdditionMode.Textpart);
MapiMessage forwardMsg = builder.buildResponse(originalMsg);
forwardMsg.save(dataDir + "forward_out.msg");

转换消息时保留空日期

MapiConversionOptions.setPreserveEmptyDates(boolean) 属性指示在转换消息时是否需要保留空日期。此 API 出现在 Aspose.Email 21.5 版。以下代码片段展示了如何保留空日期。

MailMessage mailMessage = MailMessage.load("message.eml");
System.out.println(mailMessage.getDate()); // zero date
MapiConversionOptions mco = MapiConversionOptions.getUnicodeFormat();
// keep empty dates when converting a message
mco.setPreserveEmptyDates(true);
MapiMessage mapiMessage = MapiMessage.fromMailMessage(mailMessage, mco);
System.out.println(mapiMessage.getClientSubmitTime()); // zero date
// check zero date
if (mapiMessage.getClientSubmitTime().equals(JavaHelper.ZERO_DATE))
    System.out.println("ZERO DATE");