在 Python 中创建 Outlook 分发列表

MS Outlook 允许创建分发列表以向多个人发送电子邮件,而无需编写单独的电子邮件地址。此外,您可以根据人员类型(例如官方、社交等)创建不同的列表。在以编程方式使用 MS Outlook 时,您可以在磁盘上创建并保存 PST 格式的分发列表。此 PST 文件可以在 MS Outlook 中或从您的应用程序中加载和使用。在本文中,您将学习如何在 Python 中创建 MS Outlook 分发列表。

用于创建 MS Outlook 分发列表的 Python 库

要从 Python 应用程序中创建 Outlook 分发列表,我们将使用 Aspose.Email for Python via .NET。它是一个功能丰富的库,可让您创建和发送电子邮件并与流行的电子邮件客户端无缝协作。您可以使用以下命令安装它。

> pip install Aspose.Email-for-Python-via-NET

(从 GitHub 下载完整的源代码示例包。)

用 Python 在 MS Outlook 中创建分发列表

让我们看看如何使用 Aspose.Email for Python 在 MS Outlook 中以编程方式创建分发列表。

  • 首先,创建字符串对象来存储成员的详细信息。
  • 然后,使用 PersonalStorage.create() 方法创建一个 PST 文件来存储分发列表。
  • 之后,使用 PersonalStorage.createpredefinedfolder() 方法在 PST 中创建一个新文件夹并设置其名称。
  • 然后,为每个成员创建一个 MapiDistributionListMember 对象并对其进行初始化。
  • 创建一个新的 MapiDistributionListMemberCollection 对象并向其添加成员。
  • 将集合分配给 MapiDistributionList 对象。
  • 最后,使用 addmapimessageitem() 方法将分发列表添加到 PST 的文件夹中。

以下代码示例显示了如何在 Python 中创建 MS Outlook 通讯组列表。

from aspose.email.storage.pst import *
from aspose.email.storage import PersonalStorage
from aspose.email.mapi import MapiContact
from aspose.email.mapi import MapiDistributionListMember, MapiDistributionList
from aspose.email.mapi import MapiDistributionListEntryIdType, MapiDistributionListMemberCollection
from aspose.email import StandardIpmFolder, FileFormatVersion
import base64

# 创建成员
displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"

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

# 创建 PST 文件以存储分发列表
personalStorage = PersonalStorage.create( "DistributionList.pst", FileFormatVersion.UNICODE)

# 创建文件夹
contactFolder = personalStorage.create_predefined_folder("Contacts", StandardIpmFolder.CONTACTS)

# 创建联系人
strEntryId1 = contactFolder.add_mapi_message_item(MapiContact(displayName1, email1))
strEntryId2 = contactFolder.add_mapi_message_item( MapiContact(displayName2, email2))

# 创建分发列表成员
member1 = MapiDistributionListMember(displayName1, email1)
member1.entry_id_type = MapiDistributionListEntryIdType.CONTACT
member1.entry_id = base64.b64decode( bytes(strEntryId1, "utf-8") )

member2 = MapiDistributionListMember(displayName2, email2)
member2.entry_id_type = MapiDistributionListEntryIdType.CONTACT
member2.entry_id = base64.b64decode( bytes(strEntryId1, "utf-8") )

# 将成员添加到集合
members = MapiDistributionListMemberCollection()
members.append(member1)
members.append(member2)

# 将收藏添加到列表
distribution_list = MapiDistributionList("Contact list", members)
distribution_list.body = "Distribution List Body"
distribution_list.subject = "Sample Distribution List using Aspose.Email"     
    
# 将分发列表添加到 PST 
contactFolder.add_mapi_message_item(distribution_list)

获取免费 API 许可证

您可以通过申请 免费临时许可证 通过 .NET 使用 Aspose.Email for Python。

结论

MS Outlook 中的通讯组列表可以更轻松地向一组人发送电子邮件。您可以根据收件人的类型创建任意数量的列表。在本文中,您学习了如何在 Python 中以编程方式创建 MS Outlook 分发列表。您可以简单地安装 Aspose.Email 并将提供的代码示例集成到您的 Python 应用程序中。此外,您可以使用 文档 探索有关 Python 电子邮件 API 的更多信息。此外,您可以在我们的 论坛 上发布您的问题。

也可以看看