MAPI プロパティの操作
PST フォルダー内の MAPI アイテムタイプを判定
PST ファイルには通常、メールメッセージ、カレンダーイベント、連絡先など、さまざまな種類のデータが含まれます。 MapiItemType Aspose.Email のクラスは、PST ファイル内のさまざまなタイプの MAPI アイテムにアクセスし、分類することができます。以下のコードは、PST フォルダー内の MAPI アイテムのタイプを判定する方法を示しています:
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 プロパティを取得
以下のコードサンプルは MAPI メッセージをロードし、件名やインターネットコードページなどの主要なプロパティを取得し、これらのプロパティが存在しない可能性に安全に対処します。MAPI プロパティを取得するには:
- 以下を使用してファイルから MAPI メッセージをロードします load メソッド。
- 使用する MapiPropertyTag クラスは、件名やインターネットコードページなど、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 は、MSG ファイルを操作するための API セットを提供し、名前付き MAPI プロパティの抽出も含まれます。
MSG ファイルから名前付き MAPI プロパティを読む
名前付き 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 メッセージに変換する機能を提供します。using the to_mail_message メソッド。
メッセージ変換とロードのタイムアウト設定
メッセージ変換の時間(ミリ秒)を制限するには(デフォルト値は 3 秒)、timeout プロパティ of the 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)