Làm việc với Danh bạ Outlook
Tạo, Lưu và Đọc danh bạ
Giống như MapiMessage, Aspose.Email cho phép bạn tạo liên hệ Outlook. Lớp MapiContact cung cấp tất cả các thuộc tính liên quan đến liên hệ cần thiết để tạo một liên hệ Outlook. Bài viết này cho thấy cách tạo, lưu và đọc một liên hệ Outlook bằng cách sử dụng lớp MapiContact.
Tạo và Lưu Liên hệ Outlook
Để tạo một liên hệ và lưu nó vào đĩa:
- Khởi tạo một đối tượng mới của lớp MapiContact.
- Nhập thông tin thuộc tính liên hệ.
- Thêm dữ liệu ảnh (nếu có).
- Lưu danh bạ dưới dạng MSG hoặc VCard.
Đoạn mã sau cho bạn thấy cách tạo và lưu liên hệ 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)
Lưu Liên hệ ở Định dạng VCF Phiên bản 3
Để lưu một liên hệ ở định dạng VCF Phiên bản 3, sử dụng thuộc tính version của VCardSaveOptions lớp. Tạo một thể hiện mới của lớp VCardSaveOptions, đặt thuộc tính version của đối tượng VCardSaveOptions thành VCardVersion.V30. Điều này đặt phiên bản vCard thành 3.0., gọi phương thức save của đối tượng MapiContact, truyền tên tệp "contact.vcf" và đối tượng VCardSaveOptions làm tham số. Điều này lưu liên hệ dưới dạng tệp vCard với tên tệp và tùy chọn đã chỉ định. Đoạn mã sau cho bạn thấy cách lưu một liên hệ ở định dạng VCF Phiên bản 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)
Đọc một MapiContact
Lớp MapiContact có thể được sử dụng để tải các liên hệ ở định dạng Outlook MSG và VCard. Đoạn mã sau cho bạn thấy cách tải các liên hệ Outlook lưu dưới dạng MSG và VCF vào một MapiContact.
Tải một Liên hệ từ MSG
Đoạn mã sau cho bạn thấy cách tải một liên hệ từ MSG.
Tải một Liên hệ từ VCard
Đoạn mã sau cho bạn thấy cách tải một liên hệ từ vCard.
Tải một Liên hệ từ VCard với Mã hóa Được Chỉ định
Đoạn mã sau cho bạn thấy cách tải một liên hệ từ vcard với mã hóa được chỉ định.
Lưu các mục Liên hệ VCard với Mã hóa Được chỉ định
Khi lưu tệp vCard, có thể chỉ định bộ mã ký tự sẽ được sử dụng để đảm bảo khả năng tương thích với các ký tự không phải ASCII. Đặt thuộc tính preferred_text_encoding của đối tượng VCardSaveOptions thành "utf-8". Đoạn mã sau cho thấy cách thực hiện chức năng này vào dự án của bạn:
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)
Lưu các Tệp VCard với các Trường Mở rộng
Khi lưu tệp vCard, bạn cũng có thể chỉ định các tùy chọn bao gồm việc sử dụng các trường mở rộng, tức là các thuộc tính hoặc đặc tính bổ sung có thể được thêm vào vCard ngoài tập hợp trường tiêu chuẩn được định nghĩa bởi đặc tả vCard. Thuộc tính use_extensions của VCardSaveOptions lớp cho phép bạn thực hiện điều đó. Đoạn mã sau cho thấy cách lưu một tệp VCard với các trường mở rộng:
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)
Đọc Nhiều Liên hệ ở định dạng VCard
Bạn sẽ cần các phương thức sau để lấy danh sách tất cả các liên hệ từ một 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)
Đoạn mã dưới đây sẽ minh họa quy trình đọc nhiều liên hệ từ một tệp 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")
Kết xuất thông tin danh bạ sang MHTML
Liên hệ Outlook có thể được chuyển đổi sang MHTML bằng API Aspose.Email. Ví dụ này cho thấy cách một VCard được tải vào MapiContact và sau đó chuyển sang MHTML bằng sự trợ giúp của API MailMessage.