ทำงานกับรายชื่อ Outlook

การสร้าง, บันทึกและอ่านรายชื่อติดต่อ

เช่นเดียวกับ MapiMessage, 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 ให้ใช้คุณสมบัติ version ของ VCardSaveOptions คลาส สร้างอินสแตนซ์ใหม่ของคลาส VCardSaveOptions ตั้งค่า property version ของอ็อบเจกต์ VCardSaveOptions เป็น VCardVersion.V30 ซึ่งจะกำหนดเวอร์ชัน vCard เป็น 3.0 แล้วเรียกเมธอด save ของอ็อบเจกต์ 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)

การอ่าน MapiContact

คลาส MapiContact สามารถใช้เพื่อโหลดรายชื่อในรูปแบบ Outlook MSG และ VCard ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดรายชื่อ Outlook ที่บันทึกเป็น MSG และ VCF ไปยัง MapiContact.

การโหลดรายชื่อจาก MSG

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดรายชื่อจาก MSG.

การโหลดรายชื่อจาก VCard

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดรายชื่อจาก vCard.

การโหลดรายชื่อจาก VCard ด้วยการเข้ารหัสที่ระบุ

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดรายชื่อจาก vcard ด้วยการเข้ารหัสที่ระบุ.

การบันทึกรายการติดต่อ VCard ด้วยการเข้ารหัสที่ระบุ

เมื่อบันทึกไฟล์ vCard สามารถระบุการเข้ารหัสอักขระที่ใช้เพื่อให้เข้ากันได้กับอักขระที่ไม่ใช่ ASCII ตั้งค่าคุณสมบัติ preferred_text_encoding ของอ็อบเจกต์ VCardSaveOptions เป็น "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 นอกเหนือจากชุดฟิลด์มาตรฐานที่กำหนดโดยสเปค 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 สามารถแปลงเป็น MHTML ได้โดยใช้ Aspose.Email API ตัวอย่างนี้แสดงวิธีการโหลด VCard เข้าไปใน MapiContact แล้วแปลงเป็น MHTML ด้วยความช่วยเหลือของ MailMessage API.