はじめに

Aspose.Email FOSS for Python (aspose-email-foss) は、Outlook MSG ファイル、RFC-5322 EML メッセージ、そして Compound File Binary (CFB) ストレージ構造を扱うための MIT ライセンスの Python ライブラリです。ライブラリはネイティブ拡張を必要とせず、PyPI から直接インストールできます。

このライブラリは、一般的な Python 開発のギャップである、Outlook のインストールや専用 SDK が不要で Outlook .msg ファイルの読み書きを可能にする問題に対処します。また、.msg ファイルの基盤となる完全な CFB コンテナ形式を公開しており、メッセージの移行からストレージの検査まで幅広いタスクに適しています。

このライブラリは、MAPI メッセージの構築、受信者および添付ファイルの管理、プロパティのシリアライズ、CFB ドキュメントの往復処理を網羅した型付けされた API インターフェースを提供します。


主な機能

MSG ファイルの作成とシリアライズ

MapiMessage.create() は、件名と本文文字列から新しい MAPI メッセージを構築します。そこから型付けされたプロパティ、受信者、バイナリ添付ファイルを追加でき、最終的に to_bytes() を使用して結果をバイト列にシリアライズします。

import datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId

message = MapiMessage.create("Quarterly Report", "Please review the attached report.")
message.set_property(PropertyId.SENDER_NAME, "Alice")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "alice@example.com")
message.set_property(
    PropertyId.MESSAGE_DELIVERY_TIME,
    datetime.datetime(2026, 3, 15, 10, 30, tzinfo=datetime.timezone.utc),
)
message.add_recipient("bob@example.com", display_name="Bob")
message.add_attachment("report.pdf", b"%PDF-1.4 ...", mime_type="application/pdf")

raw_bytes = message.to_bytes()

既存の MSG ファイルを読み込む

MapiMessage.from_file() はディスク上の既存の .msg ファイルを読み取ります。CFBReader を MsgReader と MsgDocument に通すことで、生のバイト列からメッセージを再構築することもできます。

from aspose.email_foss.msg.mapi_message import MapiMessage
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader

# Read from file path
msg = MapiMessage.from_file("message.msg")
print(msg.subject)

# Or reconstruct from bytes
msg2 = MapiMessage.from_msg_document(
    MsgDocument.from_reader(MsgReader(CFBReader(raw_bytes)))
)
print(msg2.subject)
for att in msg2.attachments:
    print(att.filename, len(att.data), "bytes")

MAPI と RFC-5322 EML の相互変換

MapiMessage.from_email_message() は標準的な Python email.message.EmailMessage オブジェクトを MAPI メッセージに変換し、件名、受信者、本文、添付ファイルを保持します。逆変換には to_email_message() を使用します。

from email.message import EmailMessage
from aspose.email_foss.msg.mapi_message import MapiMessage

# Build a standard Python email
eml = EmailMessage()
eml["Subject"] = "Meeting notes"
eml["From"] = "alice@example.com"
eml["To"] = "bob@example.com"
eml.set_content("Notes from today's meeting.")

# Convert to MAPI and serialize as .msg
mapi = MapiMessage.from_email_message(eml)
with open("meeting-notes.msg", "wb") as f:
    f.write(mapi.to_bytes())

# Round-trip back to EML
recovered = mapi.to_email_message()
print(recovered["Subject"])

Compound File Binary (CFB) ストレージ

このライブラリには、MAPI とは独立して Compound File Binary 形式の読み書きを行うスタンドアロンの CFB レイヤー(CFBReader, CFBWriter, CFBDocument, CFBStorage, CFBStream)が含まれています。これは、.msg ファイルの生のストレージ構造を検査したり、カスタムのコンパウンドファイル コンテナを構築したりする際に便利です。

from aspose.email_foss.cfb.writer import CFBWriter, CFBDocument, CFBStorage, CFBStream
from aspose.email_foss.cfb.reader import CFBReader

# Build a CFB document with nested storage
root = CFBStorage("Root Entry")
root.add_stream(CFBStream("Metadata", b"meta-payload"))
nested = root.add_storage(CFBStorage("Attachments"))
nested.add_stream(CFBStream("Attachment1", b"binary-content"))

doc = CFBDocument(root=root, major_version=3)
data = CFBWriter.to_bytes(doc)

# Read it back
reader = CFBReader(data)
entry = reader.resolve_path(["Attachments", "Attachment1"])
print(reader.get_stream_data(entry.stream_id))

MAPI プロパティへのアクセス

iter_properties() はメッセージ上のすべての MAPI プロパティを MapiProperty オブジェクトとして返します。名前付きプロパティ(PidLidTag と PidNameTag)は MapiNamedProperty からアクセス可能です。MapiPropertyCollection は辞書型のプロパティアクセスを提供します。

from aspose.email_foss.msg.mapi_message import MapiMessage

msg = MapiMessage.from_file("message.msg")
for prop in msg.iter_properties():
    print(hex(prop.property_id), prop.property_type, repr(prop.value)[:60])

クイックスタート

pip install aspose-email-foss
import datetime
from aspose.email_foss.msg.mapi_message import MapiMessage, PropertyId
from aspose.email_foss.msg.reader import MsgReader, MsgDocument
from aspose.email_foss.cfb.reader import CFBReader

# Create a message
message = MapiMessage.create("Hello", "This is the body.")
message.set_property(PropertyId.SENDER_NAME, "Sender")
message.set_property(PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.add_attachment("note.txt", b"attachment content", mime_type="text/plain")

# Serialize and reload
raw = message.to_bytes()
reloaded = MapiMessage.from_msg_document(
    MsgDocument.from_reader(MsgReader(CFBReader(raw)))
)
email = reloaded.to_email_message()
print("Subject:", email["Subject"])
print("From:", email["From"])

対応フォーマット

フォーマット拡張子読み取り書き込み
メッセージ.msg✓✓
CFB.cfb✓✓
eml.eml✓✓

MSG ファイルは MapiMessage.from_file() と MsgReader を介して読み取られます。EML は from_email_message() / to_email_message() 変換ブリッジを通じてアクセスできます。CFB コンテナは CFBReader と CFBWriter によって直接処理されます。


オープンソースとライセンス

Aspose.Email FOSS for Python は MIT ライセンスの下でリリースされています。ソースは GitHub で入手可能で、パッケージは PyPI を通じて aspose-email-foss という名前で配布されています。MIT ライセンスは、ライセンスの記載を除いて、ロイヤリティや帰属表示の要件なしに商用アプリケーションでの使用を許可します。


はじめに

関連リソース