はじめに
Aspose.Email FOSS for Python goes beyond basic message creation. The library exposes the full depth of the MAPI property model, letting you read, write, and query individual properties by ID and type. It handles recipients, attachments, and embedded messages as first-class objects, and provides bidirectional conversion between the MSG binary format and Python’s standard email.message.EmailMessage.
コンテナレベルでは、CFB リーダーとライターが Compound File Binary ドキュメントに対して決定的な制御を提供します。ストレージ階層をたどり、RAW ストリームデータを抽出し、ゼロから新しいコンテナを構築し、バイト列にシリアライズできます。Both CFBError および MsgError 不正な入力に対して構造化例外処理を提供します。.
この投稿では、ライブラリ自身のテストスイートとサンプルスクリプトから直接抽出した検証済みコード例を用いて、各機能を順に解説します。.
含まれるもの
MAPI プロパティアクセス
すべての MSG ファイルは MAPI プロパティ上に構築されています。. MapiMessage 公開します set_property() および get_property() プロパティをそれらの PropertyId 列挙値で。使用 iter_properties() メッセージ上のすべてのプロパティを列挙するには、または get_property_value() オプションの型デコードを伴う直接値取得のために。.
from aspose.email_foss import msg
message = msg.MapiMessage.create("Status report", "Weekly update attached.")
message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "build.agent@example.com")
message.set_property(msg.PropertyId.DISPLAY_TO, "Alice Example; Bob Example")
message.set_property(msg.PropertyId.DISPLAY_CC, "Carol Example")
message.set_property(msg.PropertyId.DISPLAY_BCC, "Ops Archive")
message.set_property(
msg.PropertyId.TRANSPORT_MESSAGE_HEADERS,
"X-Environment: production\nX-Workflow: weekly-report\n",
)
message.save("report.msg")
既存ファイルからプロパティを読み取るには:
from aspose.email_foss import msg
with msg.MapiMessage.from_file("report.msg") as loaded:
print(f"Subject: {loaded.subject}")
print(f"Body: {loaded.body}")
for prop in loaded.iter_properties():
print(f"tag=0x{prop.property_tag:08X}")
受信者と添付ファイル
受信者を追加するには add_recipient(),、表示名と受信者タイプ(To、CC、またはBCC)を指定します。ファイルを添付するには add_attachment() 生のバイトとMIMEタイプを使用します。入れ子メッセージの場合、, add_embedded_message_attachment() 完全な MapiMessage を子オブジェクトとして親の内部に埋め込みます。.
from aspose.email_foss import msg
message = msg.MapiMessage.create("Team update", "See attached notes.")
message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_recipient(
"carol@example.com",
display_name="Carol Example",
recipient_type=msg.RECIPIENT_TYPE_CC,
)
message.add_recipient(
"archive@example.com",
display_name="Ops Archive",
recipient_type=msg.RECIPIENT_TYPE_BCC,
)
message.add_attachment("notes.txt", b"Meeting notes content\n", mime_type="text/plain")
message.add_attachment("data.bin", b"\x00\x01\x02\x03", mime_type="application/octet-stream")
message.save("team-update.msg")
既存メッセージの添付ファイルを確認するには:
from aspose.email_foss import msg
with msg.MapiMessage.from_file("team-update.msg") as loaded:
for att in loaded.iter_attachments_info():
print(f"name={att.filename} mime={att.mime_type} size={len(att.data)}")
print(f" embedded={att.is_embedded_message} storage={att.storage_name}")
MSG から EmailMessage への変換
ライブラリは MSG と Python の間を双方向に変換します email.message.EmailMessage.。使用する to_email_message() 標準準拠の MIME オブジェクトを生成するには、または from_email_message() 既存の… をインポートするには EmailMessage を MSG 形式に変換します。便利なメソッド to_email_bytes() と to_email_string() をバイトまたはテキストに直接シリアライズします。.
from aspose.email_foss import msg
with msg.MapiMessage.from_file("example.msg") as loaded:
email_msg = loaded.to_email_message()
print(f"content_type: {email_msg.get_content_type()}")
print(f"is_multipart: {email_msg.is_multipart()}")
print(f"headers: {len(email_msg)}")
for key, value in email_msg.items():
print(f" {key}: {value}")
body_part = email_msg.get_body(preferencelist=("plain", "html"))
if body_part is not None:
print(f"body: {body_part.get_content()[:200]}")
CFB コンテナの往復
MSG レイヤーとは独立して、Compound File Binary コンテナを構築およびシリアライズします。. CFBDocument.from_file() 既存のコンテナを可変ドキュメントモデルにロードします with CFBStorage および CFBStream ノード。. CFBWriter.write_file() ドキュメントを決定的にディスクへシリアライズします。.
from aspose.email_foss.cfb import CFBDocument, CFBReader, CFBWriter
reader = CFBReader.from_file("container.cfb")
document = CFBDocument.from_reader(reader)
reader.close()
output = CFBWriter.to_bytes(document)
print(f"Serialized {len(output)} bytes")
エラー処理
ライブラリは例外をスローします CFBError 不正な CFB コンテンツに対して MsgError 無効な MSG 構造に対して。. MapiMessage また、次のものを提供します validation_issues 例外をスローせずに警告文字列のタプルを返すプロパティで、非準拠ファイルの取り扱いをどの程度厳格にするかを決定できるようにします。.
from aspose.email_foss.cfb import CFBReader, CFBError
from aspose.email_foss.msg import MsgReader, MsgError
try:
reader = CFBReader.from_file("corrupted.cfb")
except CFBError as e:
print(f"CFB parse error: {e}")
try:
reader = MsgReader.from_file("malformed.msg")
except MsgError as e:
print(f"MSG parse error: {e}")
クイックスタート
pip install aspose-email-foss>=26.3
既存の MSG をロードし、メタデータを読み取り、添付ファイルを一覧表示します:
from aspose.email_foss import msg
with msg.MapiMessage.from_file("inbox-message.msg") as message:
print(f"Subject: {message.subject}")
print(f"Body preview: {message.body[:200] if message.body else '(empty)'}")
for att in message.iter_attachments_info():
print(f"Attachment: {att.filename} ({att.mime_type}, {len(att.data)} bytes)")
issues = message.validation_issues
if issues:
print(f"Warnings: {issues}")
サポートされているフォーマット
| フォーマット | インポート | エクスポート |
|---|---|---|
| MSG | はい | はい |
| CFB | はい | はい |
オープンソースとライセンス
Aspose.Email FOSS for Python is released under the MIT ライセンス. 個人、社内、商用プロジェクトで制限なく使用できます。ソースコードは以下で入手可能です GitHub.