メッセージの添付ファイルの操作

Aspose Outlook での添付ファイル管理

Outlook メッセージ (MSG) ファイルの作成と保存では、メッセージの作成および保存方法、添付ファイル付き MSG ファイルの作成方法が説明されています。本記事では、Aspose.Email を使用した Microsoft Outlook 添付ファイルの管理方法を解説します。メッセージファイルからの添付ファイルは、MapiMessage クラスの Attachments プロパティを使用してアクセスし、ディスクに保存されます。Attachments プロパティは 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}")

Outlook メッセージ (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.from_file(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)

ネストされたメールメッセージ添付ファイルの取得

埋め込み OLE 添付ファイルは MapiMessage クラスの Attachment コレクションにも表示されます。以下のコード例は、埋め込みメッセージ添付ファイルを解析してディスクに保存します。MapiMessage クラスの FromProperties() 静的メソッドは、埋め込み添付ファイルから新しいメッセージを作成できます。以下のコードスニペットは、ネストされたメールメッセージ添付ファイルを取得する方法を示しています。

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 Outlook ライブラリは、Microsoft Outlook メッセージ (.msg) ファイルから添付ファイルを削除する機能を提供します。

  • RemoveAttachments() メソッドを呼び出します。メッセージファイルのパスをパラメータとして受け取ります。これは public static メソッドとして実装されているので、オブジェクトをインスタンス化する必要はありません。

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

import aspose.email as ae

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

MapiMessage クラスの静的メソッド DestoryAttachment() を呼び出すこともできます。これは RemoveAttachment() より高速に動作します。RemoveAttachment() メソッドはメッセージファイルを解析するためです。

import aspose.email as ae

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

MSG 添付ファイルの追加

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

MapiMessage に参照添付ファイルを追加する

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

Aspose.Email は、以下のコードサンプルに示すように、正しい参照添付ファイルの表示のためのツールセットを提供します。

import aspose.email as ae

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

# Add reference attachment
msg.attachments.add("Document.pdf",
    "https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
    "https://drive.google.com/drive/my-drive",
    "GoogleDrive")

# Also, you can set additional attachment properties
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PERMISSION_TYPE, int(ae.AttachmentPermissionType.ANYONE_CAN_EDIT))
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_ORIGINAL_PERMISSION_TYPE, 0)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_IS_FOLDER, False)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PROVIDER_ENDPOINT_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PREVIEW_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_THUMBNAIL_URL, "")
# Finally save the message
msg.save("msg_with_ref_attach.msg")

メッセージを添付ファイルとして埋め込む

以下のコードスニペットは、MSG ファイルに埋め込まれた Outlook MSG ファイルが、値が 5 の PR_ATTACH_METHOD を含む方法を示しています。

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.from_file(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 は、MapiAttachmentCollection の Insert メソッド (MapiAttachmentCollection Insert(int index, string name, MapiMessage msg)) を使用して、親 MSG に 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 メソッドを使用して埋め込み添付ファイルの内容を新しいものに置き換えることができます。ただし、コレクションの Count が 2 である場合に PR_ATTACH_NUM = 4 の添付ファイルを挿入することはできません。以下のコードスニペットは、添付ファイルの内容を置き換える方法を示しています。

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 の添付ファイルの名前変更

ファイルからロードしたメールメッセージの添付ファイルの表示名を変更することが可能です。以下のコードサンプルはその方法を示しています。

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"