ทำงานกับคุณสมบัติ MAPI
กำหนดประเภทรายการ MAPI ในโฟลเดอร์ PST
ไฟล์ PST ปกติจะมีข้อมูลหลายประเภท เช่น ข้อความอีเมล, เหตุการณ์ปฏิทิน, รายชื่อติดต่อ, และอื่น ๆ. MapiItemType คลาสของ Aspose.Email ช่วยให้คุณเข้าถึงและจัดประเภทของรายการ MAPI ต่างๆ ภายในไฟล์ PST โค้ดด้านล่างแสดงวิธีกำหนดประเภทของรายการ MAPI ในโฟลเดอร์ PST:
import aspose.email as ae
import aspose.pycore as pycore
pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for message_info in folder.enumerate_messages():
msg = pst.extract_message(message_info)
# Get the class type based on msg.supported_type
item_type = msg.supported_type
# Non-supported type. It cannot be accessed as appropriate item type.
if item_type == ae.mapi.MapiItemType.NONE:
print("Item type not supported")
# An email message.
elif item_type == ae.mapi.MapiItemType.MESSAGE:
# You can access to MapiMessage properties there.
# A subject for example
print(msg.subject)
# A contact item. Can be accessed as MapiContact.
elif item_type == ae.mapi.MapiItemType.CONTACT:
contact = pycore.cast(ae.mapi.MapiContact, msg.to_mapi_message_item())
# You can access to MapiContact properties there.
# A name_info.display_name for example.
print(contact.name_info.display_name)
# A calendar item. Can be accessed as MapiCalendar.
elif item_type == ae.mapi.MapiItemType.CALENDAR:
calendar = pycore.cast(ae.mapi.MapiCalendar, msg.to_mapi_message_item())
# You can access to MapiCalendar properties there.
# A location for example.
print(calendar.location)
# A distribution list. Can be accessed as MapiDistributionList.
elif item_type == ae.mapi.MapiItemType.DIST_LIST:
dlist = pycore.cast(ae.mapi.MapiDistributionList, msg.to_mapi_message_item())
# You can access to MapiDistributionList properties there
# A Journal entry. Can be accessed as MapiJournal.
elif item_type == ae.mapi.MapiItemType.JOURNAL:
journal = pycore.cast(ae.mapi.MapiJournal, msg.to_mapi_message_item())
# You can access to MapiJournal properties there
# A StickyNote. Can be accessed as MapiNote.
elif item_type == ae.mapi.MapiItemType.NOTE:
note = pycore.cast(ae.mapi.MapiNote, msg.to_mapi_message_item())
# You can access to MapiNote properties there
# A Task item. Can be accessed as MapiTask.
elif item_type == ae.mapi.MapiItemType.TASK:
task = pycore.cast(ae.mapi.MapiTask, msg.to_mapi_message_item())
# You can access to MapiTask properties there
เข้าถึงและจัดการคุณสมบัติ MAPI
นี้ MapiProperty คลาสนี้แสดงถึงคุณสมบัติ MAPI ซึ่งประกอบด้วย:
- Name: สตริงที่เป็นชื่อของคุณสมบัติ.
- Tag: จำนวนเต็มยาวที่เป็นแท็กของคุณสมบัติ.
- Data: อาเรย์ไบต์ที่เป็นตัวแทนของข้อมูลของคุณสมบัติ.
ดึงคุณสมบัติ MAPI ด้วย Property Tag
ตัวอย่างโค้ดด้านล่างโหลดข้อความ MAPI, ดึงคุณสมบัติสำคัญเช่นหัวเรื่องและหน้าโค้ดอินเทอร์เน็ต, และจัดการอย่างปลอดภัยกับความเป็นไปได้ที่คุณสมบัติเหล่านั้นอาจไม่มี เพื่อดึงคุณสมบัติ MAPI:
- โหลดข้อความ MAPI จากไฟล์โดยใช้ โหลด เมธอด.
- ใช้ แท็กคุณสมบัติ Mapi คลาสเพื่อเข้าถึงคุณสมบัติเฉพาะจากข้อความ MAPI เช่น หัวเรื่องและหน้าโค้ดอินเทอร์เน็ต.
- จัดการคุณสมบัติที่ขาดหาย.
from aspose.email.mapi import MapiMessage, MapiPropertyTag
# Load from file
msg = MapiMessage.load("message.msg")
# Access the MapiPropertyTag.PR_SUBJECT property
prop = msg.properties[MapiPropertyTag.SUBJECT]
# If the property is not found, check the MapiPropertyTag.PR_SUBJECT_W (which is a // Unicode peer of the MapiPropertyTag.PR_SUBJECT)
if prop is None:
prop = msg.properties[MapiPropertyTag.SUBJECT_W]
# Cannot found
if prop is None:
print("No property found!")
# Get the property data as string
subject = prop.get_string()
print("Subject:" + subject)
# Read internet code page property
prop = msg.properties[MapiPropertyTag.INTERNET_CPID]
if prop is not None:
print("CodePage:" + str(prop.get_long()))
ตั้งค่าคุณสมบัติ MAPI
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตั้งค่าคุณสมบัติ MAPI.
อ่านคุณสมบัติ MAPI ที่ตั้งชื่อ
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 ที่มีเนื้อหาในส่วนข้อความและไฟล์ข้อความที่แนบมา นอกจากนี้ยังแสดงวิธีการลบคุณสมบัติการแนบไฟล์จากข้อความที่สร้างขึ้น
import aspose.email as ae
# create an MSG
msg = ae.mapi.MapiMessage("from@domain.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"After removal = {msg.attachments[0].properties.count}")
แปลง MSG เป็นข้อความ MIME
Aspose.Email API ให้ความสามารถในการแปลงไฟล์ MSG เป็นข้อความ MIME โดยใช้ to_mail_message เมธอด.
การตั้งค่า Timeout สำหรับการแปลงและการโหลดข้อความ
เพื่อจำกัดเวลาเป็นมิลลิวินาทีสำหรับการแปลงข้อความ (ค่าเริ่มต้น 3 วินาที) คุณสมบัติ timeout ของ MailConversionOptions ใช้คลาส.
ต่อไปนี้คือวิธีการตั้งค่า timeout สำหรับกระบวนการแปลงและโหลดข้อความ:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
options = ae.mapi.MailConversionOptions()
options.timeout = 5000
mailMessage = msg.to_mail_message(options)
โดยการตั้งค่า timeout สำหรับกระบวนการแปลงข้อความและการโหลด คุณสามารถควบคุมเวลาสูงสุดที่การดำเนินการเหล่านี้ได้รับอนุญาตให้ทำงาน หลังจากตั้งค่า timeout แล้ว คุณสามารถดำเนินการแปลงและโหลดข้อความต่อได้
กำหนดหมวดสีสำหรับไฟล์ Outlook MSG
บางครั้งคุณอาจต้องการแยกแยะอีเมลที่สำคัญเป็นพิเศษและจัดระเบียบให้เป็นระเบียบด้วยภาพ หนังสือไลบรารีให้วิธีการทำเช่นนี้โดยการกำหนดสีเฉพาะให้กับรายการข้อความ เมื่อคุณกำหนดหมวดสีให้กับรายการ จะช่วยให้คุณระบุและค้นหารายการที่เกี่ยวข้องได้อย่างรวดเร็ว ใช้ FollowUpManager คลาสเพื่อกำหนดหมวดสีสำหรับข้อความตามที่แสดงในตัวอย่างโค้ดต่อไปนี้:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Add Two categories
ae.mapi.FollowUpManager.add_category(msg, "Purple Category")
ae.mapi.FollowUpManager.add_category(msg, "Red Category")
# Retrieve the list of available categories
categories = ae.mapi.FollowUpManager.get_categories(msg)
# Remove the specified category and then Clear all categories
ae.mapi.FollowUpManager.remove_category(msg, "Red Category")
ae.mapi.FollowUpManager.clear_categories(msg)