Làm việc với các thuộc tính MAPI

Truy cập và Đặt Thuộc tính MAPI Outlook

Lớp MapiProperty đại diện cho một thuộc tính MAPI, bao gồm:

  • Name: một chuỗi đại diện cho tên của thuộc tính.
  • Tag: một số nguyên dài đại diện cho thẻ của thuộc tính.
  • Data: một mảng byte đại diện cho dữ liệu của thuộc tính.

Lấy Thuộc tính MAPI bằng Thẻ Thuộc tính MAPI

Để lấy các thuộc tính MAPI:

  1. Tạo một thể hiện của MapiMessage bằng cách tải từ tệp hoặc luồng.
  2. Lấy MapiProperty từ MapiMessage.Properties bằng các khóa MapiPropertyTag.
  3. Lấy Dữ liệu của MapiProperty bằng phương pháp GetX.

Đoạn mã sau cho bạn thấy cách lấy thuộc tính MAPI bằng thẻ thuộc tính MAPI.

Đặt Các Thuộc tính MAPI

Đoạn mã dưới đây cho bạn thấy cách đặt các thuộc tính MAPI.

trong đó định nghĩa của phương thức convertDateTime như sau:

Đọc các thuộc tính MAPI có tên từ các tệp Outlook MSG

Aspose.Email cung cấp một bộ API để làm việc với tệp MSG, bao gồm việc trích xuất các thuộc tính MAPI có tên.

Đọc các thuộc tính MAPI có tên từ tệp MSG

Để đọc các thuộc tính MAPI có tên, chúng ta có thể sử dụng thuộc tính named_properties của MapiMessage lớp. Mẫu mã sau đây cho thấy cách tải một tin nhắn, truy xuất tất cả các thuộc tính MAPI có tên, lặp qua từng thuộc tính để kiểm tra giá trị của chúng:

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}")

Đọc Thuộc tính MAPI Được Đặt Tên từ Đính kèm

Aspose.Email cho phép truy xuất tất cả các thuộc tính MAPI có tên của một tệp đính kèm. Mẫu mã sau đây cho thấy cách đọc các thuộc tính MAPI có tên từ các tệp đính kèm:

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()}")

Xóa Thuộc tính khỏi MSG và Đính kèm

Mẫu mã này minh họa việc tạo một tin nhắn Outlook MSG với nội dung thân và một tệp tin tin nhắn đính kèm. Nó cũng trình bày cách xóa thuộc tính đính kèm khỏi tin nhắn đã tạo.

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}")