การจัดการรายการจ่ายอีเมล Outlook ในไฟล์ PST

รายการจัดส่งคือกลุ่มผู้ติดต่อที่สามารถถูกจัดการโดยซอฟต์แวร์อัตโนมัติ ซึ่งช่วยให้ผู้ใช้ส่งอีเมลถึงผู้รับหลายคนพร้อมกันได้.

Aspose.Email for Python API อนุญาตให้ผู้ใช้สร้าง, จัดการ และทำงานกับรายการจ่ายอีเมล ซึ่งรวมถึงการสร้างและบันทึกสมาชิกจากรายการ, อ่านรายการจ่ายอีเมล, ปรับปรุงคุณสมบัติของรายการ, และการดำเนินการอื่น ๆ ที่เกี่ยวข้อง.

สร้างและบันทึกรายการจ่ายอีเมลในไฟล์ PST

เพิ่มรายการจ่ายอีเมลที่มีผู้ติดต่อใน PST ที่มีอยู่

โค้ดสแนปด้านล่างแสดงวิธีสร้างรายการจ่ายอีเมลที่ประกอบด้วยผู้ติดต่อที่已จัดเก็บในไฟล์ PST แล้ว.

  1. สร้างไฟล์ PST ใหม่โดยใช้ PersonalStorage.create() ด้วยรูปแบบ UNICODE.
  2. สร้างโฟลเดอร์ "Contacts" ที่กำหนดล่วงหน้า และเพิ่มรายการผู้ติดต่อสองรายการ (Sebastian Wright และ Wichert Kroos) ลงในโฟลเดอร์นี้.
  3. เพิ่มผู้ติดต่อลงในรายการจ่ายอีเมล:
    • แต่ละผู้ติดต่อจะถูกเพิ่มลงในรายการจ่ายอีเมลเป็น MapiDistributionListMember.
    • entry_id ของแต่ละสมาชิกจะเชื่อมโยงกับผู้ติดต่อใน PST ที่สอดคล้องโดยใช้ strEntryId ที่เข้ารหัส.
  4. สร้างรายการจ่ายอีเมลชื่อ "Contact list" ซึ่งรวมผู้ติดต่อที่เพิ่มเป็นสมาชิก และเพิ่มลงในไฟล์ PST.
import aspose.email as ae

displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"

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

pst = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)

contactFolder = pst.create_predefined_folder("Contacts", ae.storage.pst.StandardIpmFolder.CONTACTS)

strEntryId1 = contactFolder.add_mapi_message_item(ae.mapi.MapiContact(displayName1, email1))
strEntryId2 = contactFolder.add_mapi_message_item(ae.mapi.MapiContact(displayName2, email2))

member1 = ae.mapi.MapiDistributionListMember(displayName1, email1)
member1.entry_id_type = ae.mapi.MapiDistributionListEntryIdType.CONTACT
member1.entry_id = strEntryId1.encode()

member2 = ae.mapi.MapiDistributionListMember(displayName2, email2)
member2.entry_id_type = ae.mapi.MapiDistributionListEntryIdType.CONTACT
member2.entry_id = strEntryId2.encode()

members = ae.mapi.MapiDistributionListMemberCollection()
members.append(member1)
members.append(member2)

distributionList = ae.mapi.MapiDistributionList("Contact list", members)
distributionList.body = "Distribution List Body"
distributionList.subject = "Sample Distribution List using Aspose.Email"
# Add distribution list to PST
contactFolder.add_mapi_message_item(distributionList)

เพิ่มรายการจ่ายอีเมลที่มีสมาชิกแบบหนึ่งครั้ง

สมาชิกแบบหนึ่งครั้งเหมาะสมเมื่อผู้ติดต่อไม่อยู่ในสมุดที่อยู่ของ Outlook แต่ยังต้องการรวมไว้ในรายการจ่ายอีเมล ตัวอย่างโค้ดต่อไปนี้แสดงวิธีสร้างรายการจ่ายอีเมลที่มีสมาชิกแบบหนึ่งครั้ง — ผู้ติดต่อที่ไม่ได้จัดเก็บในไฟล์ PST.

  1. สร้างไฟล์ PST ใหม่โดยใช้ PersonalStorage.create() ด้วยรูปแบบ UNICODE.
  2. แทนที่จะเชื่อมต่อรายชื่อจาก PST ให้กำหนดรายการผู้ติดต่อใหม่ (John R. Patrick และ Tilly Bates) โดยตรงเป็นสมาชิกแบบหนึ่งครั้ง.
  3. เพิ่มสมาชิกแบบหนึ่งครั้ง (one‑off) ไปยังรายการจ่ายอีเมล.
  4. สร้างรายการจ่ายอีเมลชื่อ "Simple list" และเพิ่มลงในโฟลเดอร์ "Contacts" ในไฟล์ PST.
import aspose.email as ae

displayName1 = "Sebastian Wright"
email1 = "SebastianWright@dayrep.com"

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

pst = ae.storage.pst.PersonalStorage.create("target.pst", ae.storage.pst.FileFormatVersion.UNICODE)

contact_folder = pst.create_predefined_folder("Contacts", ae.storage.pst.StandardIpmFolder.CONTACTS)

one_off_members = ae.mapi.MapiDistributionListMemberCollection()
one_off_members.append(ae.mapi.MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com"))
one_off_members.append(ae.mapi.MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com"))

one_off_members_list = ae.mapi.MapiDistributionList("Simple list", one_off_members)
contact_folder.add_mapi_message_item(one_off_members_list)

อ่านรายการแจกจ่ายจากไฟล์ 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)

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()
        # Now, you can work with the distribution list
        # (e.g., access its members, display its properties, or make modifications)
        for member in dist_list.members:
            print(f"{member.display_name}")

อัปเดตรายการจัดส่งในไฟล์ 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 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)