はじめに

Aspose.Email FOSS for Python is now available on PyPI: a free, MIT-licensed library for creating, reading, and converting Outlook .msg ファイルはすべてPythonで、Microsoft Officeや任意のネイティブ拡張に依存しません。インストールは以下で行います pip install aspose-email-foss>=26.3 すぐに MSG と CFB コンテナの操作を開始できます。.

このライブラリは Python 3.10 以降を対象としています。Compound File Binary (CFB) フォーマットと MSG メッセージフォーマットをゼロから実装しており、メールメッセージのシリアライズや読み取りを決定的に制御できます。実装が純粋な Python であるため、Windows、macOS、Linux、コンテナ環境でも同一に動作します。.

Aspose.Email FOSS is designed for developers building email processing pipelines, compliance archival tools, forensic analysis workflows, or any application that needs to create or inspect Outlook-compatible message files without a running Exchange or Outlook installation.


主な機能

MSG ファイルの作成

Outlook 互換の .msg ファイルをゼロから作成するには、以下を使用します MapiMessage.create().。件名、送信者、配信時間、表示フィールドなどの標準 MAPI プロパティは、 PropertyId 列挙体を使用して設定します。受信者は以下で追加します add_recipient() ファイル添付は以下で行います add_attachment(),、その後結果をディスクに保存します。.

from aspose.email_foss import msg

message = msg.MapiMessage.create(
    "Quarterly status update and rollout plan",
    "Hello team,\n\nPlease find the latest rollout summary attached.\n\nRegards,\nEngineering",
)

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.INTERNET_MESSAGE_ID, "<example-001@example.com>")

message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_recipient("bob@example.com", display_name="Bob Example")
message.add_recipient(
    "carol@example.com",
    display_name="Carol Example",
    recipient_type=msg.RECIPIENT_TYPE_CC,
)

message.add_attachment("hello.txt", b"sample attachment\n", mime_type="text/plain")
message.save("example-message.msg")

MSG の読み取りと EML への変換

既存の .msg ファイルを使用してロードします MapiMessage.from_file() そして標準的なPythonに変換します EmailMessage オブジェクトを介して to_email_message(). そこから、保存またはSMTPを介した転送のためにEMLバイトにシリアライズします。.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("example-message.msg") as loaded:
    email_message = loaded.to_email_message()
    eml_bytes = email_message.as_bytes()

with open("example-message.eml", "wb") as f:
    f.write(eml_bytes)

MSG の内部検査

使用 MsgReader およびその基礎となる CFBReader MSGファイルのバイナリ構造を検査するために。CFBメタデータ(バージョン、セクタサイズ、ディレクトリエントリ数)にアクセスし、バイナリレベルでMAPIプロパティエントリを反復処理します。.

from aspose.email_foss import msg

reader = msg.MsgReader.from_file("example-message.msg")
cfb = reader.cfb_reader

print(f"CFB major_version={cfb.major_version}")
print(f"sector_size={cfb.sector_size}")
print(f"directory_entries={cfb.directory_entry_count}")

for entry in reader.iter_top_level_fixed_length_properties():
    tag = entry.property_tag
    print(f"tag=0x{tag:08X} flags=0x{entry.flags:08X} value={entry.value.hex()}")

reader.close()

低レベル CFB アクセス

Compound File Binary コンテナを使用して読み取りおよび走査します CFBReader.ストレージとストリームを列挙し、名前でパスを解決し、カスタム処理のために生のストリームデータを抽出します。.

from aspose.email_foss.cfb import CFBReader

reader = CFBReader.from_file("example-message.msg")

for entry in reader.iter_storages():
    print(f"Storage: {entry.name}")

for entry in reader.iter_streams():
    data = reader.get_stream_data(entry.stream_id)
    print(f"Stream: {entry.name} size={len(data)}")

reader.close()

クイックスタート

ライブラリをインストールし、10 行未満で最初の MSG ファイルを作成しましょう::

pip install aspose-email-foss>=26.3
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is a test message.")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "sender@example.com")
message.add_recipient("recipient@example.com", display_name="Recipient")
message.save("hello.msg")

with msg.MapiMessage.from_file("hello.msg") as loaded:
    eml = loaded.to_email_message()
    print(eml["Subject"])

サポートされているフォーマット

フォーマットインポートエクスポート
MSGはいはい
CFBはいはい

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

Aspose.Email FOSS for Python is released under the MIT License. 個人、社内、商用プロジェクトで制限なく使用できます。ソースコードは on で入手可能です。 GitHub.


はじめに