ทำงานกับคุณสมบัติ MAPI

การเข้าถึงและตั้งค่าคุณสมบัติ Outlook MAPI

คลาส MapiProperty แสดงถึงคุณสมบัติ MAPI ซึ่งประกอบด้วย:

  • Name: สตริงที่เป็นชื่อของคุณสมบัติ.
  • Tag: จำนวนเต็มยาวที่แทนแท็กของคุณสมบัติ.
  • Data: อาเรย์ไบต์ที่เป็นตัวแทนของข้อมูลของคุณสมบัติ.

การดึงคุณสมบัติ MAPI โดยใช้แท็กคุณสมบัติ MAPI

เพื่อดึงคุณสมบัติ MAPI:

  1. สร้างอินสแตนซ์ของ MapiMessage ด้วยการโหลดจากไฟล์หรือสตรีม.
  2. ดึง MapiProperty จาก MapiMessage.Properties ด้วยคีย์ MapiPropertyTag.
  3. รับข้อมูลของ MapiProperty ด้วยเมธอด GetX.

ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการดึงคุณสมบัติ MAPI โดยใช้แท็กคุณสมบัติ MAPI.

การตั้งค่าคุณสมบัติ MAPI

โค้ดตัวอย่างต่อไปนี้แสดงวิธีตั้งค่าคุณสมบัติ MAPI.

โดยคำนิยามของเมธอด convertDateTime มีดังต่อไปนี้:

การอ่านคุณสมบัติ MAPI ที่ตั้งชื่อจากไฟล์ Outlook MSG

Aspose.Email มีชุด API เพื่อทำงานกับไฟล์ MSG รวมถึงการดึงคุณสมบัติ MAPI ที่ตั้งชื่อ

อ่านคุณสมบัติ MAPI ที่ตั้งชื่อจากไฟล์ MSG

เพื่ออ่านคุณสมบัติ MAPI ที่ตั้งชื่อ เราสามารถใช้คุณสมบัติ named_properties ของ MapiMessage คลาส ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการโหลดข้อความ ดึงคุณสมบัติ MAPI ที่ตั้งชื่อทั้งหมด และวนลูปผ่านแต่ละคุณสมบัติเพื่อตรวจสอบค่าของมัน:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("my.msg")

# Get all named MAPI properties
properties = msg.named_properties.values
# Read all properties in foreach loop
for prop in properties:
    #  Read any specific property
    if prop.descriptor.canonical_name == "PidLidSideEffects":
        print(f"{prop.descriptor.canonical_name} = {prop}")
    if prop.descriptor.canonical_name == "PidLidInternetAccountName":
        print(f"{prop.descriptor.canonical_name} = {prop}")

การอ่านคุณสมบัติ MAPI ที่มีชื่อจากไฟล์แนบ

Aspose.Email ทำให้สามารถดึงคุณสมบัติ MAPI ที่ตั้งชื่อทั้งหมดของไฟล์แนบได้ ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการอ่านคุณสมบัติ MAPI ที่ตั้งชื่อจากไฟล์แนบ:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("my.msg")

# Get all named MAPI properties
attach_properties = msg.attachments[0].named_properties.values
# Read all properties in foreach loop
for prop in attach_properties:
    #  Read any specific property
    if prop.descriptor.name == "AttachmentOriginalUrl":
        print(f"{prop.descriptor.name} = {prop.get_string()}")
    if prop.descriptor.name == "AttachmentWasSavedToCloud":
        print(f"{prop.descriptor.name} = {prop.get_boolean()}")

ลบคุณสมบัติจาก MSG และไฟล์แนบ

ตัวอย่างโค้ดนี้แสดงการสร้างข้อความ Outlook MSG พร้อมเนื้อหาในส่วน Body และไฟล์ข้อความที่แนบไว้ นอกจากนี้ยังแสดงวิธีการลบคุณสมบัติไฟล์แนบจากข้อความที่สร้างขึ้น.

import aspose.email as ae

# create an MSG
msg = ae.mapi.MapiMessage("from@doamin.com", "to@domain.com", "subject", "body");
msg.set_body_content("<html><body><h1>This is the body content</h1></body></html>", ae.mapi.BodyContentType.HTML)

# load message and add it to created MSG as attachment
attachment = ae.mapi.MapiMessage.load(attach.msg")
msg.attachments.add("Outlook2 Test subject.msg", attachment)

# count of attachment properties before removal
print(f"Before removal = {msg.attachments[0].properties.count}")

# Delete anyone attachment property
msg.attachments[0].remove_property(923467779)

# count of attachment properties after removal
print(f"Before removal = {msg.attachments[0].properties.count}")