บทนำ

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 enum. เพิ่มผู้รับด้วย 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(). จากนั้นทำการซีเรียลไลซ์เป็นไบต์ EML เพื่อการจัดเก็บหรือส่งต่อผ่าน SMTP.

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. แสดงรายการ storages และ streams, แก้ไข paths ตามชื่อ, และดึง raw stream data สำหรับการประมวลผลแบบกำหนดเอง.

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()

เริ่มต้นอย่างรวดเร็ว

ติดตั้งไลบรารีและสร้างไฟล์ 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. คุณสามารถใช้ได้ในโครงการส่วนบุคคล, ภายในองค์กร, และเชิงพาณิชย์โดยไม่มีข้อจำกัด. โค้ดต้นฉบับพร้อมให้ใช้งานบน GitHub.


เริ่มต้นใช้งาน