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

确定 PST 文件夹中的 MAPI 项目类型

PST 文件通常包含各种类型的数据,如电子邮件、日历事件、联系人等。 MapiItemType Aspose.Email 的类允许您访问并分类 PST 文件中不同类型的 MAPI 项目。下面的代码演示了如何确定 PST 文件夹中 MAPI 项目的类型:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for messageInfo in folder.enumerate_messages():
    msg = pst.extract_message(messageInfo)

    # Get the class type based on msg.SupportedType
    item_type = msg.supported_type

    # Non-supported type. It cannot be accessed as appropriate item type.
    if item_type == ae.mapi.MapiItemType.NONE:
        print("Item type not supported")
    # An email message.
    elif item_type == ae.mapi.MapiItemType.MESSAGE:
        # You can access to MapiMessage properties there.
        # A subject for example
        print(msg.subject)
    # A contact item. Can be accessed as MapiContact.
    elif item_type == ae.mapi.MapiItemType.CONTACT:
        contact = msg.to_mapi_message_item()
        # You can access to MapiContact properties there. 
        # A name_info.display_name for example. 
        print(contact.name_info.display_name)
    # A calendar item. Can be accessed as MapiCalendar.
    elif item_type == ae.mapi.MapiItemType.CALENDAR:
        calendar = msg.to_mapi_message_item()
        # You can access to MapiCalendar properties there. 
        # A location for example. 
        print(calendar.location)
    # A distribution list. Can be accessed as MapiDistributionList.
    elif item_type == ae.mapi.MapiItemType.DIST_LIST:
        dlist = msg.to_mapi_message_item()
        # You can access to MapiDistributionList properties there
    # A Journal entry. Can be accessed as MapiJournal.
    elif item_type == ae.mapi.MapiItemType.JOURNAL:
        journal = msg.to_mapi_message_item()
        # You can access to MapiJournal properties there
    # A StickyNote. Can be accessed as MapiNote.
    elif item_type == ae.mapi.MapiItemType.NOTE:
        note = msg.to_mapi_message_item()
        # You can access to MapiNote properties there
    # A Task item. Can be accessed as MapiTask.
    elif item_type == ae.mapi.MapiItemType.TASK:
        task = msg.to_mapi_message_item()
        # You can access to MapiTask properties there

将 MSG 转换为 MIME 消息

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

设置消息转换和加载的超时时间

要将消息转换的时间限制为毫秒(默认值 3 秒),请使用 timeout 属性 MailConversionOptions 类被使用。

以下是为消息转换和加载过程设置超时的方法:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("my.msg")
options = ae.mapi.MailConversionOptions()
options.timeout = 5000
mailMessage = msg.to_mail_message(options)

通过为消息转换和加载过程设置超时时间,您可以控制这些操作允许运行的最大时间。设置超时时间后,您即可继续执行消息转换和加载过程。

Outlook 模板(OFT)文件处理

加载、修改并保存 OFT 文件

Outlook 模板(OFT)提供了一种简化重复发送电子邮件的便捷方式。您无需每次从头撰写相同的邮件,只需在 Outlook 中创建消息并保存为 Outlook 模板(OFT)。以后需要发送类似邮件时,可快速从模板生成。此方式省去重复编写邮件正文、设置主题、格式化等工作。

Aspose.Email 的 MailMessage 类提供了强大的工具来加载和操作 Outlook 模板文件(OFT)。一旦将 Outlook 模板加载到 MailMessage 类的实例中,您即可轻松更新发件人、收件人、邮件正文、主题以及其他各种属性。

import aspose.email as ae

# Load the OFT file
oft_file_path = "your_template.oft"
mail_message = ae.MailMessage.load(oft_file_path)

# Update properties as needed
mail_message.subject = "Updated Subject"
mail_message.body = "Updated body text."

# Save the updated message as an MSG file
msg_file_path = "updated_message.msg"
mail_message.save(msg_file_path, ae.MailMessageSaveType.outlook_message_format_unicode)

print(f"Updated message saved to {msg_file_path}")

完成必要的更新后,您可以使用以下方式发送电子邮件 SmtpClient 类:

# Send the email
smtpClient.send(message)

将 Outlook MSG 文件保存为模板

为此,您可以使用 MailMessage 类用于创建电子邮件,然后将其保存为 OFT 文件。下面的代码示例展示了如何将电子邮件保存为 Outlook 模板:

import aspose.email as ae

# Create a new MailMessage
message = ae.MailMessage()
message.subject = "Sample Outlook Template"
message.html_body = "<html><body>This is the body of the email.</body></html>"
message.from_address = ae.MailAddress("your.email@example.com", "Your Name")

# Save the MailMessage as an Outlook Template (OFT) file
oft_file_path = "sample_template.oft"
message.save(oft_file_path, ae.SaveOptions.default_oft)

print(f"Outlook Template saved as {oft_file_path}")

OFT 还是 MSG:确定 MapiMessage 的类型

下面的代码片段说明了如何判断已加载的 MAPI 消息是标准电子邮件还是 Outlook 模板(OFT):

msg = ae.mapi.MapiMessage.Load("message.msg");
isOft = msg.is_template # returns false

msg = ae.mapi.MapiMessage.Load("message.oft");
isOft = msg.IsTemplate; # returns true

加载 MSG 文件后,代码检查 is_template 属性是否为 MapiMessaage 类返回 True 或 False。如果返回 false,则加载的消息是标准电子邮件,而不是 Outlook 模板;如果返回 true,则该消息不是标准电子邮件,而是 Outlook 模板。

将 MapiMessage 或 MailMessage 保存为 OFT

SaveOptions 是一个抽象基类,允许用户在将 MailMessage 保存为特定格式时指定附加选项。下面的代码示例演示了如何将消息保存为 OFT 格式:

import aspose.email as ae

# Save the MailMessage to OFT format
eml = ae.MailMessage.load("message.eml")

eml.save("message.oft", ae.SaveOptions.default_oft)

# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)

# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)

# Save the MapiMessage to OFT format
msg = ae.mapi.MapiMessage.load("message.msg")

msg.save("message.oft", ae.SaveOptions.default_oft)

# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)

# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)

管理带数字签名的消息

该库提供了 LoadOptions 类,是允许用户在从特定格式加载 MailMessage 时指定附加选项的抽象基类。默认启用保留签名的选项。

从 EML 转换为 MSG 并保留签名

下面的代码示例演示了如何加载消息、将其转换为 MSG 格式并保存为 MSG(默认保留签名):


import aspose.email as ae

# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)

将 S/MIME 消息从 MSG 转换为 EML

Aspose.Email 允许您将 MSG 转换为 EML,同时保留数字签名,如以下代码片段所示。

import aspose.email as ae

# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)

conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")

为 Outlook MSG 文件设置颜色类别

有时您可能需要区分重要性不同的电子邮件并对其进行视觉组织。该库提供了一种方法,通过为消息项分配特定颜色来实现。当您为项目设置颜色类别时,可以一目了然地轻松识别和定位相关项目。使用 FollowUpManager 类用于设置消息的颜色分类,如以下代码示例所示:


import aspose.email as ae

msg = ae.mapi.MapiMessage.load("my.msg")

# Add Two categories
ae.mapi.FollowUpManager.add_category(msg, "Purple Category")
ae.mapi.FollowUpManager.add_category(msg, "Red Category")

# Retrieve the list of available categories
categories = ae.mapi.FollowUpManager.get_categories(msg)

# Remove the specified category and then Clear all categories
ae.mapi.FollowUpManager.remove_category(msg, "Red Category")
ae.mapi.FollowUpManager.clear_categories(msg)

从 MSG 文件访问后续信息

提取已读和送达回执信息

下面的代码示例演示了如何从 Outlook 消息(MSG)文件中提取收件人信息及其跟踪状态:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("received.msg")

for recipient in msg.recipients:
    print(f"Recipient: {recipient.display_name}")
    print(f"Delivery time:  {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_DELIVERY]}")
    print(f"Read time:  {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_READ]}")

创建转发和回复消息

Aspose.Email 提供了基于现有邮件创建转发和回复消息的简便方法。

创建转发消息

您可以使用 ForwardMessageBuilder 该类用于通过设置源消息、发件人、收件人、主题和正文来创建转发邮件。转发邮件可以将原始消息作为附件或作为转发邮件的正文。您可以灵活自定义附件、标题和格式等附加属性。以下代码示例展示了如何创建转发邮件:


import aspose.email as ae

msg = ae.mapi.MapiMessage.load("original.msg")

builder = ae.tools.ForwardMessageBuilder()
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART

forwardMsg = builder.build_response(msg)
forwardMsg.save("forward_out.msg")

创建回复消息

ReplyMessageBuilder 该类用于配置回复设置,包括源消息、发件人、收件人、回复模式、主题前缀以及回复消息正文。可根据需求创建不同回复模式的回复邮件,如 “回复发件人” 或 “全部回复”。您可以自定义附件、标题和格式选项等属性。以下代码示例展示了如何创建转发邮件:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("original.msg")

builder = ae.tools.ReplyMessageBuilder()

# Set ReplyMessageBuilder Properties
builder.reply_all = True
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
builder.response_text = "<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>";

replyMsg = builder.build_response(msg)
replyMsg.save("reply_out.msg")