使用 Outlook 联系人
创建、保存和读取联系人
与 MapiMessage 类似,Aspose.Email 允许创建 Outlook 联系人。MapiContact 类提供创建 Outlook 联系人所需的所有属性。本文展示如何使用 MapiContact 类创建、保存和读取 Outlook 联系人。
创建并保存 Outlook 联系人
创建联系人并保存到磁盘:
- 实例化一个新的 MapiContact 类对象。
- 输入联系人属性信息。
- 添加照片数据(如果有)。
- 将联系人保存为 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.0 版式保存联系人
要将联系人保存为 VCF 格式版本 3,请使用 VCardSaveOptions 类。创建 VCardSaveOptions 类的新实例,将其 version 属性设为 VCardVersion.V30,以将 vCard 版本设为 3.0。调用 MapiContact 对象的 save 方法,传入文件名 "contact.vcf" 和 VCardSaveOptions 对象作为参数。这会将联系人保存为指定文件名和选项的 vCard 文件。以下代码片段展示如何以 VCF 3.0 版本保存联系人:
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)
读取 MapiContact
MapiContact 类可用于加载 Outlook MSG 和 VCard 格式的联系人。以下代码片段展示如何将保存为 MSG 和 VCF 的 Outlook 联系人加载到 MapiContact 中。
从 MSG 加载联系人
以下代码片段展示如何从 MSG 加载联系人。
从 VCard 加载联系人
以下代码片段展示如何从 vCard 加载联系人。
使用指定编码从 VCard 加载联系人
以下代码片段展示如何使用指定编码从 vCard 加载联系人。
使用指定编码保存 VCard 联系人项
保存 vCard 文件时,可以指定使用的字符编码,以确保兼容非 ASCII 字符。将 VCardSaveOptions 对象的 preferred_text_encoding 属性设置为 "utf-8"。以下代码片段展示如何在项目中实现此功能:
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 中添加的标准字段之外的附加属性或特性。use_extensions 属性用于 VCardSaveOptions 该类允许您实现此功能。以下代码片段展示如何使用扩展字段保存 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 中所有联系人的列表:
# Checks whether VCard source stream contains multiple contacts.
aspose.email.personalinfo.vcard.VCardContact.is_multi_contacts(stream)
# Loads list of all contacts from VCard file.
aspose.email.personalinfo.vcard.VCardContact.load_as_multiple(file_path, encoding)
# Loads list of all contacts from VCard stream.
aspose.email.personalinfo.vcard.VCardContact.load_as_multiple(stream, encoding)
以下代码片段演示从 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
Outlook 联系人可以使用 Aspose.Email API 转换为 MHTML。本示例展示如何将 VCard 加载到 MapiContact,然后借助 MailMessage API 转换为 MHTML。