Outlook 連絡先の操作

連絡先の作成、保存、読み取り

MapiMessage と同様に、Aspose.Email は Outlook 連絡先の作成を可能にします。MapiContact クラスは、Outlook 連絡先を作成するために必要なすべての連絡先関連プロパティを提供します。本記事では、MapiContact クラスを使用して Outlook 連絡先を作成、保存、読み取る方法を示します。

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)

バージョン 3 の VCF フォーマットで連絡先を保存

VCF フォーマット バージョン 3 で連絡先を保存するには、 VCardSaveOptions class. VCardSaveOptions クラスの新しいインスタンスを作成し、VCardSaveOptions オブジェクトの version プロパティを VCardVersion.V30 に設定します。これにより vCard のバージョンが 3.0 に設定されます。その後、MapiContact オブジェクトの save メソッドを呼び出し、ファイル名 "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)

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 ファイルを保存する際、標準のフィールドセットに加えて追加できる拡張フィールド(追加のプロパティや属性)を使用するオプションも指定できます。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 に変換する方法を示しています。