Outlook メッセージの添付ファイル管理

この Outlook メッセージ(MSG)ファイルの作成と保存 この記事では、添付ファイル付き MSG ファイルを含むメッセージの作成と保存の手順を説明しました。この記事では、Aspose.Email を使用した Microsoft Outlook の添付ファイル管理について掘り下げます。メッセージファイルから添付ファイルにアクセスし、保存する方法を以下を利用して案内します。 attachments プロパティ( MapiMessage クラス。このプロパティは、以下に属するコレクションです。 MapiAttachmentCollection クラス、添付ファイルの効率的な処理を可能にします。

インラインとレギュラーの添付を確認

"Inline" と "Regular" の添付は、メールメッセージへの含め方を指します。Regular 添付は従来通りにファイルを添付する方式で、通常はメールクライアント内のリストに表示され、受信者がダウンロードしてローカルに保存できます。Inline 添付は、埋め込み画像やインライン画像とも呼ばれ、メール本文内に画像やメディアを組み込む際に使用されます。別個のリストには表示されず、メール本文の内容として直接表示されます。これにより、メッセージ本文の一部として画像やメディアを組み込むことができます。以下のコードサンプルは、添付がインラインかレギュラーかを判定する方法を示しています。

import aspose.email as ae

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

for attachment in msg.attachments:
    print(f"{attachment.display_name}:{attachment.is_inline}")

MAPI メッセージでの参照添付ファイルを識別

Aspose.Email API には以下が含まれます。 is_reference プロパティ( MapiAttachment クラスは、メッセージ内の参照添付ファイルを識別できるようにします。以下のコードサンプルで Python プロジェクトでの動作を確認してください。

import aspose.email as ae

# Load or create a MapiMessage
msg = ae.mapi.MapiMessage.load("Message_With_Reference_Attachment.msg")

# Iterate through the attachments
for attachment in msg.attachments:
    if attachment.is_reference:
        # Process reference attachment
        pass

MSG ファイルから添付ファイルを保存

MSG ファイルから添付ファイルを保存するには:

  1. 次を反復処理します MapiAttachmentCollection コレクションから個々の添付ファイルを取得します。
  2. 添付ファイルを保存するには、次を呼び出します。 MapiAttachment クラス Save() メソッド。

以下のコードスニペットは、添付ファイルをローカルディスクに保存する方法を示しています。

import aspose.email as ae

data_dir = "C://dataDir/"
file_name = "message.msg"

# Create an instance of MapiMessage from file
message = ae.mapi.MapiMessage.load(data_dir + file_name)

# Iterate through the attachments collection
for attachment in message.attachments:
    # Save the individual attachment
    attachment.save(data_dir + attachment.file_name)

TNEF 添付ファイルの取り扱い

TNEF 形式での添付ファイルの読み込みと保存

Aspose.Email は TNEF 添付ファイルを処理するメソッドを提供します。これらのメソッドにより、Outlook メッセージ(winmail.dat)で一般的に使用される TNEF 形式で添付ファイルを保存・読み込むことができます。

以下のコードサンプルは、メモリストリームまたはファイルを通じて TNEF 形式のメール添付ファイルを抽出、保存、再追加する方法を示しています。

import io
from aspose.email.mapi import MapiMessage, MapiAttachment

# Load the EML message (which contains a winmail.dat TNEF attachment)
msg = MapiMessage.load("message.eml")

# Save the first attachment as a winmail.dat file
msg.attachments[0].save_to_tnef("winmail.dat")

# Load the TNEF attachment from the file and add it back to the message
from_tnef_attachment = MapiAttachment.load_from_tnef("winmail.dat")
msg.attachments.add(from_tnef_attachment.long_file_name, from_tnef_attachment.binary_data)

# (Optional) If you also want to use a memory stream instead of the file
ms = io.BytesIO()
msg.attachments[0].save_to_tnef(ms)

# Reset the stream position to the beginning
ms.seek(0)

# Load the TNEF attachment directly from the memory stream and add it
from_tnef_attachment_from_stream = MapiAttachment.load_from_tnef(ms)
msg.attachments.add(from_tnef_attachment_from_stream.long_file_name, from_tnef_attachment_from_stream.binary_data)

入れ子になったメールメッセージ添付を取得

埋め込み OLE(Object Linking and Embedding)添付ファイルは、添付コレクションにも表示されます。 MapiMessage クラス。以下のコード例は、メッセージファイルを解析し、埋め込みメッセージ添付ファイルを抽出し、さらにそれらをディスクに保存して後で使用できるようにする方法を示しています。 MapiMessage クラスは a を提供します。 from_properties 埋め込み添付ファイルから新しいメッセージを作成する静的メソッド。以下のコードスニペットは、入れ子になったメールメッセージの添付を取得する方法を示しています。

import aspose.email as ae

eml = ae.mapi.MapiMessage.load("my.msg")

# Create a MapiMessage object from the individual attachment
get_attachment = ae.mapi.MapiMessage.from_properties(eml.attachments[0].object_data.properties)

# Create an object of type MailMessageInterpreter from the above message and save the embedded message to a file on disk
mail_message = get_attachment.to_mail_message(ae.mapi.MailConversionOptions())
mail_message.save("NestedMailMessageAttachments_out.eml", ae.SaveOptions.default_eml)

添付ファイルを削除

Aspose.Email ライブラリは、Microsoft Outlook メッセージ(.msg)ファイルから添付ファイルを削除する機能を提供します。次を使用してください。 remove_attachments(path) このメソッドはメッセージファイルのパスをパラメータとして要求します。パブリックな静的メソッドであるため、オブジェクトをインスタンス化する必要なく直接呼び出すことができ、プロセスがシンプルで効率的です。

以下のコードスニペットは、Aspose.Email を使用して MSG ファイルから添付ファイルを削除する方法を示しています。

import aspose.email as ae

ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")

また、静的 destroy_attachments メソッドを呼び出すこともできます。 MapiMessage クラス。remove_attachments よりも高速に動作します。なぜなら remove_attachments メソッドはメッセージファイルを解析するためです。

import aspose.email as ae

# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments("AttachmentsToDestroy_out.msg")

MSG 添付ファイルを追加

Outlook メッセージは、添付ファイルとして通常または埋め込みの形で他の Microsoft Outlook メッセージを含めることができます。 MapiAttachmentCollection のオーバーロードされたメンバーを提供します。 追加 両方のタイプの添付ファイルを持つ Outlook メッセージを作成するメソッド。

MAPI メッセージに参照添付を追加

参照添付 は、実際のファイルではなく外部リソースへの参照やリンクを含む添付を指します。このような参照は、HTML メールで外部サーバーにホストされた画像やリソースへのリンクとして使用されることが多いです。ファイル全体を埋め込む代わりに、参照添付は外部コンテンツへの URL または参照を含みます。

MAPI メッセージに参照添付を追加するには、次を使用します。 add メソッド( MapiAttachmentCollection class と ReferenceAttachmentOptions 指定するクラス:

  • コンテンツ URL(ファイルへの直接リンク)

  • プロバイダー URL(ファイルの場所または UI ビューへのリンク)

  • 添付プロバイダー名(例: "GoogleDrive")

以下のコードサンプルに示すように。

import aspose.email as ae

# Load or create a MapiMessage
msg = ae.mapi.MapiMessage()
msg.subject = "Email with Reference Attachment"
msg.body = "This message contains a cloud-based reference attachment."

# Create ReferenceAttachmentOptions
options = ae.mapi.ReferenceAttachmentOptions(
    "https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
    "https://drive.google.com/drive/my-drive",
    "GoogleDrive"
)

# Add the reference attachment to the message
msg.attachments.add("Document.pdf", options)

# Save the message to MSG format
msg.save("Message_With_Reference_Attachment.msg")

メッセージを添付として埋め込む

以下のコードスニペットは、Outlook MSG ファイルを別の MSG ファイルの添付として埋め込む方法を示しています。埋め込み MSG ファイルは PR_ATTACH_METHOD プロパティに値 5 を持ち、タイプが "message" の添付であることを示します。

import aspose.email as ae

# Create a new MapiMessage
message = ae.mapi.MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body")

# Load the attachment message
attach_msg = ae.mapi.MapiMessage.load("Message.msg")

# Add the attachment to the message
message.attachments.add("Weekly report.msg", attach_msg)

# Save the message with the embedded message attachment
message.save("WithEmbeddedMsg_out.msg")

添付ファイルから埋め込みメッセージを読む

以下のコードスニペットは、添付ファイルから埋め込みメッセージを読み取る方法を示しています。

import aspose.email as ae

file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Check if the first attachment is an Outlook message
if message.attachments[0].object_data.is_outlook_message:
    # Get the embedded message as MapiMessage
    embedded_message = message.attachments[0].object_data.to_mapi_message()
    # Perform further operations with the embedded message
    # ...

添付ファイルの挿入と置換

Aspose.Email API は、親メッセージ内の特定のインデックスに添付ファイルを挿入する機能を提供します。さらに、既存の添付ファイルの内容を別のメッセージ添付に置換することも可能です。以下に、添付ファイルの挿入と置換を行うコードスニペットを示します。

特定の位置に添付ファイルを挿入

Aspose.Email API を使用すると、Insert メソッドを使って MSG 添付ファイルを親 MSG に挿入できます。 MapiAttachmentCollection。このメソッドはシグネチャ MapiAttachmentCollection Insert(int index, string name, MapiMessage msg) で使用されます。以下のコードスニペットは、特定の位置に添付を挿入する方法を示しています。

import aspose.email as ae
from io import BytesIO

file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)

# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)

# Insert the loaded attachment at index 1
message.attachments.insert(1, "new 11", get_data)

添付ファイルの内容を置換

次を使用できます。 replace 埋め込み添付ファイルの内容を新しいデータで更新するメソッドです。ただし、このメソッドは PR_ATTACH_NUM = 4 の添付ファイルを、2 つしか添付できないコレクションに挿入することには使用できません。以下は添付ファイルの内容を置き換えるコードスニペットです:

import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"

# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)

# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)

# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)

# Replace the attachment at index 1 with the loaded attachment
message.attachments.replace(1, "new 1", get_data)

MapiMessage の添付ファイル名を変更

Aspose.Email を使用すれば、ファイルからロードしたメールメッセージの添付ファイルの表示名を簡単に変更できます。以下のコードサンプルは、その方法を示しています。

import aspose.email as ae

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

msg.attachments[0].display_name = "New display name 1"
msg.attachments[1].display_name = "New display name 2"