在 PST 文件中管理 Outlook 分发列表

通讯组列表是一组联系人,可通过自动化软件进行操作,使用户能够一次性向多个收件人发送电子邮件。

Aspose.Email for Python API 允许用户创建、管理和操作分发列表。包括从列表创建并保存成员、读取分发列表、更新列表属性以及其他相关操作。

在 PST 文件中创建和保存分发列表

添加带已有 PST 联系人的分发列表

下面的代码片段演示了如何创建包含已存储在 PST 文件中的联系人的分发列表。

  1. 使用以下方式创建新 PST 文件 PersonalStorage.create() 使用 UNICODE 格式。
  2. 创建预定义的 “Contacts” 文件夹,并向该文件夹添加两个联系人条目(Sebastian Wright 和 Wichert Kroos)。
  3. 将联系人添加到分发列表:
    • 每个联系人作为一个 MapiDistributionListMember.
    • 每个成员的 entry_id 通过编码的 strEntryId 与相应的 PST 联系人关联。
  4. 创建一个名为“Contact list”的分发列表,将已添加的联系人作为成员,并将其添加到 PST 文件中。
import aspose.email as ae

displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"

displayName2 = "Wichert Kroos"
email2 = "WichertKroos@teleworm.us"

pst = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)

contactFolder = pst.create_predefined_folder("Contacts", ae.storage.pst.StandardIpmFolder.CONTACTS)

strEntryId1 = contactFolder.add_mapi_message_item(ae.mapi.MapiContact(displayName1, email1))
strEntryId2 = contactFolder.add_mapi_message_item(ae.mapi.MapiContact(displayName2, email2))

member1 = ae.mapi.MapiDistributionListMember(displayName1, email1)
member1.entry_id_type = ae.mapi.MapiDistributionListEntryIdType.CONTACT
member1.entry_id = strEntryId1.encode()

member2 = ae.mapi.MapiDistributionListMember(displayName2, email2)
member2.entry_id_type = ae.mapi.MapiDistributionListEntryIdType.CONTACT
member2.entry_id = strEntryId2.encode()

members = ae.mapi.MapiDistributionListMemberCollection()
members.append(member1)
members.append(member2)

distributionList = ae.mapi.MapiDistributionList("Contact list", members)
distributionList.body = "Distribution List Body"
distributionList.subject = "Sample Distribution List using Aspose.Email"
# Add distribution list to PST
contactFolder.add_mapi_message_item(distributionList)

添加带一次性成员的分发列表

当联系人不在 Outlook 通讯录中但仍需加入分发列表时,可使用一次性成员。下面的代码示例演示了如何使用一次性成员创建分发列表——这些联系人未存储在 PST 文件中。

  1. 使用以下方式创建新 PST 文件 PersonalStorage.create() 使用 UNICODE 格式。
  2. 不从 PST 链接联系人,而是直接将新联系人条目(John R. Patrick 和 Tilly Bates)定义为一次性成员。
  3. 将一次性成员添加到分发列表中。
  4. 创建一个名为“Simple list”的分发列表,并将其添加到 PST 文件中的 “Contacts” 文件夹。
import aspose.email as ae

displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"

displayName2 = "Wichert Kroos"
email2 = "WichertKroos@teleworm.us"

pst = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)

contact_folder = pst.create_predefined_folder("Contacts", ae.storage.pst.StandardIpmFolder.CONTACTS)

one_off_members = ae.mapi.MapiDistributionListMemberCollection()
one_off_members.append(ae.mapi.MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com"))
one_off_members.append(ae.mapi.MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com"))

one_off_members_list = ae.mapi.MapiDistributionList("Simple list", one_off_members)
contact_folder.add_mapi_message_item(one_off_members_list)

从 PST 文件读取通讯组列表

要从 PST 中读取通讯组列表,请使用以下代码示例:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.CONTACTS)

for msg in folder.enumerate_messages():
    # Check if the message has the "IPM.DistList" message class
    if msg.message_class == "IPM.DistList":
        dist_list = pst.extract_message(msg).to_mapi_message_item()
        # Now, you can work with the distribution list
        # (e.g., access its members, display its properties, or make modifications)
        for member in dist_list.members:
            print(f"{member.display_name}")

更新 Outlook PST 文件中的分发列表

要在 PST 文件中更新通讯组列表,例如添加新成员,请使用以下代码示例:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.CONTACTS)

# add a new member to each distribution list in pst
for msg in folder.enumerate_messages():
    # Check if the message has the "IPM.DistList" message class
    if msg.message_class == "IPM.DistList":
        dist_list = pst.extract_message(msg).to_mapi_message_item()
        # Create new member to add
        member = ae.mapi.MapiDistributionListMember("Edward R. Manuel", "EdwardRManuel@example.com")
        dist_list.members.append(member)
        # update DL in PST
        folder.update_message(msg.entry_id_string, dist_list)