在 PST 文件中管理 Outlook 联系人

创建、保存和读取联系人

Aspose.Email 允许您创建和处理 Outlook 联系人。 MapiContact 类提供创建和操作 Outlook 联系人所需的所有联系属性。本文展示了如何使用该类创建、保存和读取 Outlook 联系人。 MapiContact 类。

创建并保存 Outlook 联系人

创建联系人并保存到磁盘,请按以下步骤操作:

  1. 实例化一个新的对象 MapiContact 类。
  2. 输入联系人属性信息。
  3. 添加照片数据(如果有)。
  4. 将联系人保存为 MSG 或 VCard 格式。

下面的代码片段展示了如何创建并保存 Outlook 联系人:

import aspose.email as ae

data_dir = "path/to/data/directory"

contact = ae.mapi.MapiContact()
contact.name_info = ae.mapi.MapiContactNamePropertySet("Bertha", "A.", "Buell")
contact.professional_info = ae.mapi.MapiContactProfessionalPropertySet("Awthentikz", "Social work assistant")
contact.personal_info.personal_home_page = "B2BTies.com"
contact.physical_addresses.work_address.address = "Im Astenfeld 59 8580 EDELSCHROTT"
contact.electronic_addresses.email1 = ae.mapi.MapiContactElectronicAddress("Experwas", "SMTP", "BerthaABuell@armyspy.com")
contact.telephones = ae.mapi.MapiContactTelephonePropertySet("06605045265")
contact.personal_info.children = ["child1", "child2", "child3"]
contact.categories = ["category1", "category2", "category3"]
contact.mileage = "Some test mileage"
contact.billing = "Test billing information"
contact.other_fields.journal = True
contact.other_fields.private = True
contact.other_fields.reminder_topic = "Test topic"
contact.other_fields.user_field1 = "ContactUserField1"
contact.other_fields.user_field2 = "ContactUserField2"
contact.other_fields.user_field3 = "ContactUserField3"
contact.other_fields.user_field4 = "ContactUserField4"

# Add a photo
with open(data_dir + "Desert.jpg", "rb") as file:
    buffer = file.read()
    contact.photo = ae.mapi.MapiContactPhoto(buffer, ae.mapi.MapiContactPhotoImageFormat.JPEG)

# Save the Contact in MSG format
contact.save(data_dir + "MapiContact_out.msg", ae.mapi.ContactSaveFormat.MSG)

# Save the Contact in VCF format
contact.save(data_dir + "MapiContact_out.vcf", ae.mapi.ContactSaveFormat.V_CARD)

以 VCF 格式版本 3 保存联系人

要将联系人保存为 VCF 格式版本 3,请使用 VCardSaveOptions 类。

  1. 创建该类的新实例 VCardSaveOptions 类。
  2. 将 VCardSaveOptions 对象的 version 属性设置为 VCardVersion.V30。这将 vCard 版本设为 3.0。
  3. 调用 保存 MapiContact 对象的方法,传入文件名 "contact.vcf" 和 VCardSaveOptions 对象作为参数。这会将联系人保存为指定文件名和选项的 vCard 文件。

下面的代码片段展示了如何将联系人保存为 VCF 格式版本 3:

import aspose.email as ae

contact = ae.mapi.MapiContact()
contact.name_info = ae.mapi.MapiContactNamePropertySet("Bertha", "A.", "Buell")
options = ae.personalinfo.vcard.VCardSaveOptions()
options.version = ae.personalinfo.vcard.VCardVersion.V30
contact.save("contact.vcf", options)

从 MSG 或 VCard 文件加载联系人

该 MapiContact 类可用于加载 Outlook MSG 和 VCard 格式的联系人。

要 从 MSG 加载联系人,请使用下面的代码示例:

要 从 VCard 加载联系人,请使用以下代码示例:

使用指定编码加载联系人

下面的代码片段展示了如何使用指定编码加载 vcard 联系人。

使用自定义字符编码保存 VCard 文件

保存 vCard 文件时,您可以定义字符编码,以确保兼容非 ASCII 字符。例如,将 VCardSaveOptions 对象设置为 "utf-8" 可确保非 ASCII 文本的正确编码。此功能对于处理多语言联系人数据尤为有用。下面是配置并使用自定义字符编码保存 vCard 文件的示例:

import aspose.email as ae

contact = ae.mapi.MapiContact()
contact.name_info = ae.mapi.MapiContactNamePropertySet("Bertha", "A.", "Buell")
options = ae.personalinfo.vcard.VCardSaveOptions()
options.preferred_text_encoding = "utf-8"
contact.save("contact.vcf", options)

使用扩展属性保存 VCard 文件

除了标准 vCard 字段,您还可以通过包含扩展属性来增强 vCard 文件。这些是标准 vCard 规范未覆盖的额外属性,允许更大的定制。要启用此功能,请设置 VCardSaveOptions 类设置为 True。这对于添加自定义或特定应用程序的元数据尤为有益。以下是如何在启用扩展属性的情况下保存 vCard 文件:

import aspose.email as ae

contact = ae.mapi.MapiContact()
contact.name_info = ae.mapi.MapiContactNamePropertySet("Bertha", "A.", "Buell")
options = ae.personalinfo.vcard.VCardSaveOptions()
options.use_extensions = True
contact.save("contact.vcf", options)

以 VCard 格式读取多个联系人

要检索 VCard 中的所有联系人,您可以使用以下 方法:

  • is_multi_contacts 方法判断流中是否包含多个联系人。
  • load_as_multiple(file_path) 方法从 VCard 文件加载所有联系人的列表。
  • load_as_multiple(stream) 方法从 VCard 流加载所有联系人的列表。

下面的代码片段演示了从 VCard 文件读取多个联系人的过程:

import aspose.email as ae

contact = ae.mapi.MapiContact()
contact.name_info = ae.mapi.MapiContactNamePropertySet("Bertha", "A.", "Buell")
options = ae.personalinfo.vcard.VCardSaveOptions()
options.use_extensions = True
contact.save("contact.vcf", options)

if ae.personalinfo.vcard.VCardContact.is_multi_contacts("contact.vcf"):
    ae.personalinfo.vcard.VCardContact.load_as_multiple("contact.vcf")

将联系信息渲染为 MHTML

可以使用 Aspose.Email API 将 Outlook 联系人转换为 MHTML。以下代码示例演示如何生成包含原始 MSG 文件详细联系信息的 MHTML 文件:

  1. 将 MSG 文件加载为 MapiMessage 对象。
  2. 将消息转换为 EML 格式。
  3. 准备 MHT 保存选项:
    • 启用正文内容编码并保留原始边界。
    • 设置 MHT 格式选项以写入标题并渲染 vCard 信息。
  4. 定义在 MHT 中渲染的字段。
  5. 将 EML 保存为 MHTML 文件。