MSG ファイルのロードと解析

このセクションでは、Microsoft Outlook メッセージ ファイル(.msg)のロードと解析方法を説明します。Aspose.Email API は MapiMessage 複数のシナリオ向けに静的ロード関数を備えた MSG ファイルをロードするクラスです。以下のコードスニペットは、ファイルまたはストリームから MSG ファイルをロードする方法を示しています。

ディスクから MSG ファイルをロード

以下のコードスニペットは、ディスクから MSG ファイルをロードする方法を示しています。

from aspose.email.mapi import MapiMessage

# Create an instance of MapiMessage from file
msg = MapiMessage.load("message.msg")

# Get subject
print("Subject: " + msg.subject)

# Get from address
print("From: " + msg.sender_email_address)

# Get body
print("Body: " + msg.body)

# Get recipients information
recipients = ", ".join([r.email_address for r in msg.recipients])
print("Recipients: " + recipients)

# Get attachments
for att in msg.attachments:
    print(att.file_name)
    print(att.display_name)

ストリームから MSG ファイルをロード

以下のコードスニペットは、ストリームから MSG ファイルをロードする方法を示しています。

from aspose.email.mapi import MapiMessage
import io

# Read the file into a byte array
file_path = "message.msg"
with open(file_path, "rb") as file:
    bytes_data = file.read()

# Create a memory stream from the byte array
stream = io.BytesIO(bytes_data)
stream.seek(0)

# Create an instance of MapiMessage from the stream
msg = MapiMessage.load(stream)

# Get subject
print("Subject: " + msg.subject)

# Get from address
print("From: " + msg.sender_email_address)

# Get body
print("Body: " + msg.body)

埋め込み EML 形式を保持したまま EML を MSG に変換

EML ファイルは以下にロードできます。 MapiMessage クラスにインスタンス化して MailMessage オブジェクトを作成し、 MapiMessage.from_mail_message メソッドです。EML ファイルに埋め込みファイルが含まれる場合は、次を使用してください。 MapiConversionOptions.preserve_embedded_message_format 埋め込みファイルの形式を保持するために、以下のコードスニペットは埋め込み EML ファイルの形式を保持したまま EML ファイルを MapiMessage にロードする方法を示しています。

以下のコードスニペットは、埋め込みメッセージ形式を保持したまま EML ファイルを MSG ファイルに変換する方法を示しています。

from aspose.email import MailMessage, EmlLoadOptions
from aspose.email.mapi import MapiMessage, MapiConversionOptions, OutlookMessageFormat

eml_file = "message.eml"

# Load the EML file
eml_options = EmlLoadOptions()
eml = MailMessage.load(eml_file, eml_options)

# Create MapiConversionOptions
conversion_options = MapiConversionOptions()
conversion_options.format = OutlookMessageFormat.UNICODE

# Preserve Embedded Message Format
conversion_options.preserve_embedded_message_format = True

# Convert EML to MSG with options
msg = MapiMessage.from_mail_message(eml, conversion_options)