使用 MAPI 属性

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

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

import aspose.email as ae
import aspose.pycore as pycore

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

    # Get the class type based on msg.supported_type
    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 = pycore.cast(ae.mapi.MapiContact, 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 = pycore.cast(ae.mapi.MapiCalendar, 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 = pycore.cast(ae.mapi.MapiDistributionList, 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 = pycore.cast(ae.mapi.MapiJournal, 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 = pycore.cast(ae.mapi.MapiNote, 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 = pycore.cast(ae.mapi.MapiTask, msg.to_mapi_message_item())
        # You can access to MapiTask properties there

访问和管理 MAPI 属性

该 MapiProperty 类表示一个 MAPI 属性,包含:

  • Name:表示属性名称的字符串。
  • Tag:表示属性标记的长整数。
  • Data:表示属性数据的字节数组。

使用属性标记获取 MAPI 属性

以下代码示例加载 MAPI 消息,检索主题和互联网代码页等关键属性,并安全地处理这些属性可能缺失的情况。获取 MAPI 属性:

  1. 使用以下方式从文件加载 MAPI 消息 load 方法。
  2. 使用 MapiPropertyTag 类用于访问 MAPI 消息的特定属性,如主题和互联网代码页。
  3. 处理缺失的属性。
from aspose.email.mapi import MapiMessage, MapiPropertyTag

# Load from file
msg = MapiMessage.load("message.msg")

# Access the MapiPropertyTag.PR_SUBJECT property
prop = msg.properties[MapiPropertyTag.SUBJECT]

# If the property is not found, check the MapiPropertyTag.PR_SUBJECT_W (which is a // Unicode peer of the MapiPropertyTag.PR_SUBJECT)
if prop is None:
	prop = msg.properties[MapiPropertyTag.SUBJECT_W]

# Cannot found
if prop is None:
	print("No property found!")

# Get the property data as string
subject = prop.get_string()

print("Subject:" + subject)

# Read internet code page property
prop = msg.properties[MapiPropertyTag.INTERNET_CPID]
if prop is not None:
	print("CodePage:" + str(prop.get_long()))

设置 MAPI 属性

以下代码片段展示了如何设置 MAPI 属性。

读取已命名的 MAPI 属性

Aspose.Email 提供了一套用于处理 MSG 文件的 API,包括提取已命名的 MAPI 属性。

从 MSG 文件读取已命名的 MAPI 属性

要读取已命名的 MAPI 属性,您可以使用 named_properties 属性的 MapiMessage 类。以下代码示例展示了如何加载消息,检索所有已命名的 MAPI 属性,遍历每个属性以检查其值:

import aspose.email as ae

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

# Get all named MAPI properties
properties = msg.named_properties.values
# Read all properties in foreach loop
for prop in properties:
    #  Read any specific property
    if prop.descriptor.canonical_name == "PidLidSideEffects":
        print(f"{prop.descriptor.canonical_name} = {prop}")
    if prop.descriptor.canonical_name == "PidLidInternetAccountName":
        print(f"{prop.descriptor.canonical_name} = {prop}")

从附件读取已命名的 MAPI 属性

Aspose.Email 可以检索附件的所有已命名 MAPI 属性。以下代码示例展示了如何从附件中读取已命名的 MAPI 属性:

import aspose.email as ae

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

# Get all named MAPI properties
attach_properties = msg.attachments[0].named_properties.values
# Read all properties in foreach loop
for prop in attach_properties:
    #  Read any specific property
    if prop.descriptor.name == "AttachmentOriginalUrl":
        print(f"{prop.descriptor.name} = {prop.get_string()}")
    if prop.descriptor.name == "AttachmentWasSavedToCloud":
        print(f"{prop.descriptor.name} = {prop.get_boolean()}")

属性管理

从 MSG 文件和附件中删除属性

下面的代码示例演示了创建包含正文内容和附件消息文件的 Outlook MSG 消息。它还展示了如何删除已创建消息中的附件属性。

import aspose.email as ae

# create an MSG
msg = ae.mapi.MapiMessage("from@domain.com", "to@domain.com", "subject", "body")
msg.set_body_content("<html><body><h1>This is the body content</h1></body></html>", ae.mapi.BodyContentType.HTML)

# load message and add it to created MSG as attachment
attachment = ae.mapi.MapiMessage.load("attach.msg")
msg.attachments.add("Outlook2 Test subject.msg", attachment)

# count of attachment properties before removal
print(f"Before removal = {msg.attachments[0].properties.count}")

# Delete anyone attachment property
msg.attachments[0].remove_property(923467779)

# count of attachment properties after removal
print(f"After removal = {msg.attachments[0].properties.count}")

将 MSG 转换为 MIME 消息

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

为消息转换和加载设置超时

要将消息转换的时间限制为毫秒(默认值 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 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)