Aspose.Email.Outlook を使用したメッセージファイルの管理

PST フォルダー内の MAPI アイテムタイプを判定する

PST ファイルには通常、メールメッセージ、カレンダーイベント、連絡先など、さまざまな種類のデータが含まれます。 MapiItemType Aspose.Email のクラスは、PST ファイル内のさまざまなタイプの MAPI アイテムにアクセスし、分類することができます。以下のコードは、PST フォルダー内の MAPI アイテムのタイプを判定する方法を示しています:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("test.pst")
folder = pst.root_folder.get_sub_folder("Calendar")
for messageInfo in folder.enumerate_messages():
    msg = pst.extract_message(messageInfo)

    # Get the class type based on msg.SupportedType
    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 = 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 = 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 = 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 = 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 = 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 = msg.to_mapi_message_item()
        # You can access to MapiTask properties there

MSG を MIME メッセージに変換

Aspose.Email API は、ToMailMessage メソッドを使用して MSG ファイルを MIME メッセージに変換する機能を提供します。

メッセージの変換とロードのタイムアウト設定

メッセージ変換の時間(ミリ秒)を制限するには(デフォルト値は 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 テンプレート (OFT) ファイルの処理

OFT ファイルのロード、変更、保存

Outlook テンプレート (OFT) は、繰り返し送信するメールメッセージのプロセスを効率化する便利な方法を提供します。同じメールを毎回最初から作成する代わりに、Outlook でメッセージを作成し、OFT として保存できます。その後、同様のメッセージを送信する必要があるときに、テンプレートからすぐに生成できます。この方法により、メッセージ本文の内容や件名、書式設定などを再度書く手間が省けます。

Aspose.Email の MailMessage クラスは Outlook テンプレートファイル (OFT) をロードおよび操作する強力なツールを提供します。Outlook テンプレートが MailMessage クラスのインスタンスにロードされると、送信者、受信者、メッセージ本文、件名、その他さまざまな属性を簡単に更新できます。

import aspose.email as ae

# Load the OFT file
oft_file_path = "your_template.oft"
mail_message = ae.MailMessage.load(oft_file_path)

# Update properties as needed
mail_message.subject = "Updated Subject"
mail_message.body = "Updated body text."

# Save the updated message as an MSG file
msg_file_path = "updated_message.msg"
mail_message.save(msg_file_path, ae.MailMessageSaveType.outlook_message_format_unicode)

print(f"Updated message saved to {msg_file_path}")

必要な更新を行った後、以下を使用してメールを送信できます SmtpClient クラス:

# Send the email
smtpClient.send(message)

Outlook MSG ファイルをテンプレートとして保存

この目的のために、次のものを使用できます: MailMessage クラスはメールを作成し、OFT ファイルとして保存します。以下のコードサンプルは、メールを Outlook テンプレートとして保存する方法を示しています。

import aspose.email as ae

# Create a new MailMessage
message = ae.MailMessage()
message.subject = "Sample Outlook Template"
message.html_body = "<html><body>This is the body of the email.</body></html>"
message.from_address = ae.MailAddress("your.email@example.com", "Your Name")

# Save the MailMessage as an Outlook Template (OFT) file
oft_file_path = "sample_template.oft"
message.save(oft_file_path, ae.SaveOptions.default_oft)

print(f"Outlook Template saved as {oft_file_path}")

OFT または MSG:MapiMessage のタイプを判定する

以下のコードスニペットは、ロードされた MAPI メッセージが標準のメールメッセージか Outlook テンプレート (OFT) かを判定する方法を示しています。

msg = ae.mapi.MapiMessage.Load("message.msg");
isOft = msg.is_template # returns false

msg = ae.mapi.MapiMessage.Load("message.oft");
isOft = msg.IsTemplate; # returns true

MSG ファイルをロードした後、コードは is_template プロパティをチェックします MapiMessage クラスは True または False です。false を返す場合、ロードされたメッセージは標準のメールメッセージであり、Outlook テンプレートではありません。true を返す場合、メッセージは標準メールではなく、Outlook テンプレートです。

MapiMessage または MailMessage を OFT として保存する

この SaveOptions クラスは、MailMessage を特定の形式で保存する際に追加オプションを指定できる抽象基底クラスです。以下のコードサンプルは、メッセージを OFT 形式で保存する方法を示しています。

import aspose.email as ae

# Save the MailMessage to OFT format
eml = ae.MailMessage.load("message.eml")

eml.save("message.oft", ae.SaveOptions.default_oft)

# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)

# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
eml.save("message.oft", save_options)

# Save the MapiMessage to OFT format
msg = ae.mapi.MapiMessage.load("message.msg")

msg.save("message.oft", ae.SaveOptions.default_oft)

# or alternative way 2
save_options = ae.MsgSaveOptions(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)

# or alternative way 3
save_options = ae.SaveOptions.create_save_options(ae.MailMessageSaveType.outlook_template_format)
msg.save("message.oft", save_options)

デジタル署名付きメッセージの管理

ライブラリは以下を提供します。 LoadOptions クラスは、特定の形式から MailMessage をロードする際に追加オプションを指定できる抽象基底クラスです。署名保持オプションはデフォルトで設定されています。

署名を保持したまま EML から MSG へ変換

以下のコードサンプルは、メッセージをロードし、MSG 形式に変換して MSG として保存する方法を示しています(署名はデフォルトで保持されます)。


import aspose.email as ae

# Load mail message
loadOptions = ae.EmlLoadOptions()
message = ae.MailMessage.load("Message.eml", loadOptions)
# Save as MSG
message.save("ConvertEMLToMSG_out.msg", ae.SaveOptions.default_msg_unicode)

MSG から EML への S/MIME メッセージ変換

Aspose.Email を使用すると、以下のコードスニペットに示すように、デジタル署名を保持したまま MSG から EML へ変換できます。

import aspose.email as ae

# Load mail message
loadOptions = ae.EmlLoadOptions()
smime_eml = ae.MailMessage.load("smime.eml", loadOptions)

conversion_options = ae.mapi.MapiConversionOptions(ae.mapi.OutlookMessageFormat.UNICODE)
msg = ae.mapi.MapiMessage.from_mail_message(smime_eml, conversion_options)
# Save File to disk
msg.save("ConvertMIMEMessagesFromMSGToEML_out.msg")

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)

MSG ファイルからフォローアップ情報にアクセスする

既読および配信確認情報の抽出

以下のコードサンプルは、Outlook メッセージ(MSG)ファイルから受信者情報とその追跡ステータスを抽出する方法を示しています。

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("received.msg")

for recipient in msg.recipients:
    print(f"Recipient: {recipient.display_name}")
    print(f"Delivery time:  {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_DELIVERY]}")
    print(f"Read time:  {recipient.properties[ae.mapi.MapiPropertyTag.RECIPIENT_TRACKSTATUS_TIME_READ]}")

転送メッセージと返信メッセージの作成

Aspose.Email は、既存のメッセージを基に転送メッセージと返信メッセージを作成する簡単な方法を提供します。

転送メッセージの作成

次を使用できます。 ForwardMessageBuilder クラスは、元メッセージ、送信者、受信者、件名、本文を設定して転送メッセージを作成します。転送メッセージは、元のメッセージを添付ファイルとして、あるいは転送メッセージの本文として含めることができます。添付ファイル、ヘッダー、書式設定オプションなど、追加のプロパティを柔軟にカスタマイズできます。以下のコードサンプルは、転送メッセージの作成方法を示しています:


import aspose.email as ae

msg = ae.mapi.MapiMessage.load("original.msg")

builder = ae.tools.ForwardMessageBuilder()
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART

forwardMsg = builder.build_response(msg)
forwardMsg.save("forward_out.msg")

返信メッセージの作成

この ReplyMessageBuilder クラスは、元メッセージ、送信者、受信者、返信モード、件名のプレフィックス、返信メッセージ本文などの返信設定を構成するために使用されます。要件に応じて「送信者への返信」や「全員への返信」などの異なる返信モードで返信メッセージを作成できます。添付ファイル、ヘッダー、書式設定オプションなど、返信メッセージのさまざまなプロパティをカスタマイズできます。以下のコードサンプルは、転送メッセージの作成方法を示しています:

import aspose.email as ae

msg = ae.mapi.MapiMessage.load("original.msg")

builder = ae.tools.ReplyMessageBuilder()

# Set ReplyMessageBuilder Properties
builder.reply_all = True
builder.addition_mode = ae.tools.OriginalMessageAdditionMode.TEXTPART
builder.response_text = "<p><b>Dear Friend,</b></p> I want to do is introduce my co-author and co-teacher. <p><a href=\"www.google.com\">This is a first link</a></p><p><a href=\"www.google.com\">This is a second link</a></p>";

replyMsg = builder.build_response(msg)
replyMsg.save("reply_out.msg")