MAPI Özellikleriyle Çalışma

Bir PST Klasöründeki MAPI Öğesi Türünü Belirle

Bir PST dosyası tipik olarak e-posta mesajları, takvim etkinlikleri, kişiler ve daha fazlası gibi çeşitli veri türleri içerir. MapiItemType Aspose.Email sınıfı, bir PST dosyası içinde farklı MAPI öğelerinin türlerine erişmenizi ve sınıflandırmanızı sağlar. Aşağıdaki kod bir PST klasöründeki bir MAPI öğesinin türünü belirlemeyi gösterir:

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 Özelliklerine Eriş ve Yönet

Bu MapiProperty sınıf, şu öğeleri içeren bir MAPI özelliğini temsil eder:

  • Name: özelliğin adını temsil eden bir dize.
  • Tag: özelliğin etiketini temsil eden uzun bir tamsayı.
  • Data: özelliğin verisini temsil eden bir bayt dizisi.

Özellik Etiketi Kullanarak MAPI Özelliği Al

Aşağıdaki kod örneği bir MAPI mesajını yükler, konu ve internet kod sayfası gibi temel özellikleri alır ve bu özelliklerin potansiyel yokluğunu güvenli bir şekilde ele alır. MAPI özelliklerini almak için:

  1. Bir dosyadan MAPI mesajını şu şekilde yükleyin: yükle metod.
  2. Kullan MapiPropertyTag bir MAPI mesajından konu ve internet kod sayfası gibi belirli özelliklere erişmek için sınıf.
  3. Eksik özellikleri ele al.
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 Özelliklerini Ayarla

Aşağıdaki kod örneği MAPI özelliklerini nasıl ayarlayacağınızı gösterir.

Adlandırılmış MAPI Özelliklerini Okuma

Aspose.Email, adlandırılmış MAPI özelliklerinin çıkarılmasını da içeren MSG dosyaları ile çalışmak için bir dizi API sağlar.

MSG Dosyalarından Adlandırılmış MAPI Özelliklerini Okuma

Adlandırılmış MAPI özelliklerini okumak için şunu kullanabilirsiniz: named_properties özellik MapiMessage sınıfı. Aşağıdaki kod örneği, bir mesajın nasıl yükleneceğini, tüm adlandırılmış MAPI özelliklerinin nasıl alınacağını ve her özelliğin değerinin nasıl kontrol edileceğini gösterir:

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

Eklerden Adlandırılmış MAPI Özelliklerini Okuma

Aspose.Email, bir ekin tüm adlandırılmış MAPI özelliklerini almayı mümkün kılar. Aşağıdaki kod örneği, eklerden adlandırılmış MAPI özelliklerinin nasıl okunacağını göstermektedir:

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

Özellik Yönetimi

MSG Dosyalarından ve Eklerden Özellikleri Kaldırma

Aşağıdaki kod örneği, gövde içeriği ve ekli bir mesaj dosyası ile bir Outlook MSG mesajının oluşturulmasını göstermektedir. Ayrıca, oluşturulan mesajdan bir ek özelliğinin nasıl silineceğini de gösterir.

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’yi MIME Mesajına Dönüştürme

Aspose.Email API, MSG dosyasını MIME mesajına dönüştürme yeteneğini aşağıdakini kullanarak sağlar to_mail_message metod.

Mesaj Dönüştürme ve Yükleme için Zaman Aşımı Ayarlama

Mesaj dönüştürme süresini milisaniye cinsinden sınırlamak için (varsayılan değer 3 saniye), timeout özelliği: MailConversionOptions sınıf kullanılır.

İşte mesaj dönüştürme ve yükleme işlemleri için zaman aşımı nasıl ayarlanır:

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)

Mesaj dönüştürme ve yükleme işlemleri için bir zaman aşımı ayarlayarak, bu işlemlerin çalışabileceği maksimum süreyi kontrol edebilirsiniz. Zaman aşımı ayarlandıktan sonra mesaj dönüştürme ve yükleme işlemlerinize devam edebilirsiniz.

Outlook MSG Dosyalarına Renk Kategorisi Ayarlama

Bazen belirli öneme sahip e-postaları ayırmak ve görsel olarak düzenlemek isteyebilirsiniz. Kütüphane, bir mesaj öğesine belirli bir renk atayarak bunu yapmanın bir yolunu sunar. Bir öğeye renk kategorisi atadığınızda, ilgili öğeleri bir bakışta kolayca tanımlayıp bulabilirsiniz. Kullan: FollowUpManager sınıf, aşağıdaki kod örneğinde gösterildiği gibi bir mesaj için renk kategorisi ayarlamak için kullanılır:

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)