PST ファイル内の Outlook 連絡先の管理
Outlook 連絡先を PST ファイルに追加
新規 PST ファイルの作成とサブフォルダーの追加 PST ファイルを作成しサブフォルダーを含める方法を示しています。Aspose.Email を使用すると、作成またはロードした PST ファイルの Contacts サブフォルダーに MapiContact を追加できます。以下は PST ファイルに MapiContact を追加する手順です。
- 作成する MapiContact オブジェクト。
- 異なるコンストラクタやメソッドを使用して、名前、性別、メールアドレス、電話番号、住所、職業情報などの MapiContact プロパティを設定します。
- 次を使用して PST を作成します PersonalStorage.create() メソッド。
- ルートフォルダーにアクセスし、次に呼び出すことで、PST ファイルのルートに事前定義されたフォルダー(Contacts)を作成します。 add_mapi_message_item() メソッド。
以下のコードスニペットは、MAPI 連絡先を作成し、新規作成した PST ファイルの Contacts フォルダーに追加する方法を示しています。
Outlook 連絡先を MSG ファイルとして保存
Outlook PST ファイルから連絡先情報にアクセスし、MSG 形式でディスクに保存するために、Aspose.Email は以下を提供します PersonalStorage および MapiContact クラスです。以下のコードスニペットは、PST ファイルからすべての連絡先情報を取得し、MSG 形式でディスクに保存する方法を示しています。
- PST ファイルを次でロードします PersonalStorage クラス。
- Contacts フォルダーを参照します。
- Contacts フォルダーの内容を取得してメッセージ コレクションを取得します。
- メッセージ コレクションをループ処理します。
- 呼び出す PersonalStorage.extract_message() 連絡先情報を取得するメソッド MapiMessage クラス。
- 呼び出す MapiMessage.save() 連絡先を MSG 形式でディスクに保存するメソッドです。
Outlook 連絡先を VCF ファイルとしてエクスポート
Microsoft Outlook PST ファイルから連絡先情報にアクセスし、vCard (VCF) 形式でディスクに保存するには、次を使用します PersonalStorage および MapiContact クラスです。以下のコードはディスクから PST ファイルをロードし、すべての連絡先を vCard (VCF) 形式で保存します。VCF ファイルは標準の vCard 連絡先ファイルをロードできる任意のプログラムで使用できます。Microsoft Outlook で任意の VCF ファイルを開くと、下のスクリーンショットのように表示されます。
![]() |
|---|
以下のコードスニペットは、Outlook PST から連絡先を vCard (VCF) 形式にエクスポートする方法を示しています。
- 使用する PersonalStorage.from_file PST ファイルを開くために。
- Contacts フォルダーにアクセスするには get_sub_folder.
- 連絡先をループ処理:
- 使用する get_contents() フォルダー内のすべてのメッセージ情報を取得するために。
- 繰り返し処理 message_info_collection ループで。
- 各連絡先を抽出するには使用 pst.extract_message(message_info) そしてそれを MAPI メッセージ アイテムとして保存します。
- 各連絡先の名前とエントリ ID を出力します。
- VCF ファイルとして連絡先を保存するには使用 contact.save.
from aspose.email.storage.pst import PersonalStorage
from aspose.email.mapi import ContactSaveFormat
# Load the Outlook PST file
pst = PersonalStorage.from_file("my.pst")
# Get the Contacts folder
folder_info = pst.root_folder.get_sub_folder("Contacts")
# Loop through all the contacts in this folder
message_info_collection = folder_info.get_contents()
for message_info in message_info_collection:
# Get the contact information
contact = pst.extract_message(message_info).to_mapi_message_item()
# Display some contents on screen
print("Name: " + contact.name_info.display_name + " - " + message_info.entry_id_string)
# Save to disk in vCard VCF format
contact.save("D:\\" + contact.name_info.display_name + ".vcf", ContactSaveFormat.V_CARD)
Outlook 配布リストの PST ファイルでの管理
Aspose.Email for Python API を使用すると、複数の連絡先のコレクションである配布リストを作成できます。配布リストは Outlook MSG 形式でディスクに保存でき、MS Outlook で開いて表示/操作できます。
配布リストの作成と保存
以下のコードスニペットは、PST ファイルを作成し配布リストを追加する方法を示しています。また、PST ファイル内の配布リストに連絡先を作成・追加することも含まれています。
- 連絡先の詳細を定義 - 各連絡先の displayName と email を設定します。
- 次を使用して新しい PST ファイルを作成します: PersonalStorage.create() UNICODE 形式で。
- Contacts フォルダーを作成するには使用 create_predefined_folder().
- インスタンス化 MapiContact 表示名とメールアドレスを持つオブジェクトを作成し、次を使用してフォルダーに連絡先を追加します add_mapi_message_item().
- インスタンス化して配布リストメンバーを作成します MapiDistributionListMember 各連絡先に対して、base64 デコードで entry_id を設定します。
- メンバーを追加する MapiDistributionListMemberCollection.
- インスタンス化して配布リストを作成します MapiDistributionList、本文と件名を設定します。
- 使用する add_mapi_message_item() 配布リストを連絡先フォルダーに追加するために。
displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"
displayName2 = "Wichert Kroos"
email2 = "WichertKroos@teleworm.us"
personalStorage = PersonalStorage.create(dataDir + "CreateDistributionListInPST_out.pst", FileFormatVersion.UNICODE)
contactFolder = personalStorage.create_predefined_folder("Contacts", StandardIpmFolder.CONTACTS)
# Create 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"
# Add distribution list to PST
contactFolder.add_mapi_message_item(distribution_list);
PST ファイルから配布リストを読み取る
以下のコードスニペットは、PST ファイルから配布リストを読み取る方法を示しています。
from aspose.email.mapi import MapiMessage
# Load the MAPI message from file
message = MapiMessage.load("dl.msg")
# Convert the message to MAPI distribution list
dlist = message.to_mapi_message_item()
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 a 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)
