العمل مع خصائص 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، والتي تحتوي على:

  • الاسم: سلسلة تمثل اسم الخاصية.
  • الوسم: عدد صحيح طويل يمثل وسم الخاصية.
  • البيانات: مصفوفة بايت تمثل بيانات الخاصية.

الحصول على خاصية MAPI باستخدام وسم الخاصية

عينة الشفرة أدناه تقوم بتحميل رسالة MAPI، استرجاع الخصائص الرئيسية مثل العنوان وصفحة الكود الإنترنت، وتعالج بأمان احتمال غياب هذه الخصائص. للحصول على خصائص MAPI:

  1. تحميل رسالة MAPI من ملف باستخدام تحميل طريقة.
  2. استخدم الـ MapiPropertyTag فئة للوصول إلى خصائص معينة من رسالة MAPI، مثل العنوان وصفحة الكود الإنترنت.
  3. معالجة الخصائص المفقودة.
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 مجموعة من واجهات البرمجة للعمل مع ملفات 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 طريقة.

ضبط المهلة الزمنية لتحويل الرسائل وتحميلها

للحد من الوقت بالمللي ثانية لتحويل الرسالة (القيمة الافتراضية 3 ثوانٍ)، خاصية timeout في MailConversionOptions يتم استخدام الفئة.

إليك كيفية ضبط مهلة زمنية لعمليات تحويل الرسائل وتحميلها:

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)

من خلال ضبط مهلة زمنية لعمليات تحويل الرسائل وتحميلها، يمكنك التحكم في الحد الأقصى للوقت المسموح لهذه العمليات بالتنفيذ. بعد ضبط المهلة، يمكنك المتابعة في عمليات تحويل الرسائل وتحميلها.

تعيين فئة اللون لملفات 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)