Introduction

Aspose.Email FOSS for Python is a free, MIT-licensed library for creating, reading, and การแปลงไฟล์ข้อความที่เข้ากับ Outlook ใน Python สะอาด. มีใน PyPI เป็น aspose-email-foss, มันทํางานบน Windows, macOS, Linux, Docker และสอร์เวอร์ลิสแวดล้อม โดยไม่มีการพึ่งพา Microsoft Office และ ไม่มีเวลาทํางานของเจ้าสิทธิ์.

ห้องสมุดครอบคลุม 2 ด้านหลัก คือ ชั้น MAPI ระดับสูง (MapiMessage) สําหรับการทํางาน กับสารประกอบด้วยหัวข้อ, ร่างกาย, ผู้รับและเอกสารแนบ และระบบโครงสร้างระดับต่ํา ชั้น (MsgReader, CFBReader, CFBStorage, CFBStream) ที่เปิดเผยสารสกัดสด ไบนารีคอนเทนเนอร์ที่รองรับทุกไฟล์ .msg แฟ้ม.

บทความนี้เดินผ่านลักษณะหลักที่ส่งในเวอร์ชั่น 26.3 ที่เนื่องจากพื้นผิว API ที่ได้รับการยืนยัน.


คุณสมบัติหลัก

การสร้างข้อความระดับสูงด้วย MapiMessage

MapiMessage เป็นจุดเข้าหลักสําหรับการสร้างข้อความที่สอดคล้องกับ Outlook จาก โทรหาฉันนะ MapiMessage.create() กับหัวข้อและร่างกาย, เพิ่มผู้รับ และ ผสมต่อ แล้วเก็บตรงไปยัง .msg แฟ้ม.

from aspose.email_foss import msg

message = msg.MapiMessage.create(
    "Quarterly status update",
    "Hello team,\n\nPlease review the attached summary.\n\nRegards,\nEngineering",
)

message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "agent@example.com")

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_attachment("report.txt", b"Summary content here.\n", mime_type="text/plain")
message.save("output.msg")

การ อ่าน และ ตรวจสอบ ไฟล์ MSG

MsgReader ให้การเข้าถึงระบบที่มีอยู่อย่างจัดระเบียบ .msg ไฟล์โดยไม่อัตราการโหลดเต็ม เป็นตัวแทนของ MapiMessage.มันเปิดเผยรายการของทรัพย์สินระดับสูงสุด, การเก็บรักษาผู้รับ และ การเก็บของติดต่อแต่ละครั้ง ทําให้เป็นไปตามวิธีการในการตรวจสอบไฟล์ขนาดใหญ่และ ช่องทางการตรวจสอบความถูกต้อง.

with msg.MsgReader.from_file(“output.msg”) as reader: header = reader.top_level_header print(f"Recipients : {header.recipient_count}") print(f"Attachments: {header.attachment_count}")

for entry in reader.iter_recipient_storages():
    _, props = reader.parse_subobject_property_stream(entry.stream_id)
    print(f"  Recipient storage {entry.name}: {len(props)} property entries")

```python
from aspose.email_foss import msg

with msg.MsgReader.from_file("output.msg") as reader:
    header = reader.top_level_header
    print(f"Recipients : {header.recipient_count}")
    print(f"Attachments: {header.attachment_count}")

    for entry in reader.iter_recipient_storages():
        _, props = reader.parse_subobject_property_stream(entry.stream_id)
        print(f"  Recipient storage {entry.name}: {len(props)} property entries")

MSG ทัวร์กลับ: อัตราบรรจุ ปรับเปลี่ยน เก็บไว้

Because MapiMessage การสนับสนุน from_file() สําหรับการบรรทุก และ save() สําหรับการเขียน, เต็ม วงจรการอ่าน-ปรับเปลี่ยน-เขียนต้องการเพียงไม่กี่แถว. คุณสมบัติที่มีชื่อ, enums ที่ถูกตีและ การจัดการรหัสยูนิโคดมีให้บริการผ่าน set_property, PropertyId,และ MapiNamedProperty.

with msg.MapiMessage.from_file(“original.msg”) as message: message.set_property(msg.PropertyId.SUBJECT, “Updated Subject”) message.save(“updated.msg”)


```python
from aspose.email_foss import msg

with msg.MapiMessage.from_file("original.msg") as message:
    message.set_property(msg.PropertyId.SUBJECT, "Updated Subject")
    message.save("updated.msg")

EML / RFC 5322 การแปลง

MapiMessage.to_email_message() เปลี่ยน MSG ที่ถูกบรรจุเป็น Python มาตรฐาน email.message.EmailMessage อัตถุ, ทําให้สามารถส่งออกไปยัง RFC 5322 .eml การใช้รูปแบบ ห้องสมุดมาตรฐาน. ทางกลับ (MapiMessage.from_email_message()) สร้าง a การทําการ MapiMessage จากการ EmailMessage.

from pathlib import Path
from aspose.email_foss import msg

with msg.MapiMessage.from_file("message.msg") as message:
    eml_bytes = message.to_email_bytes()
    Path("message.eml").write_bytes(eml_bytes)

# Reverse: load EML into MapiMessage
import email
raw_eml = Path("message.eml").read_bytes()
email_obj = email.message_from_bytes(raw_eml)
mapi = msg.MapiMessage.from_email_message(email_obj)
mapi.save("from_eml.msg")

CFB การเดินผ่านคอนเทนเนอร์

Every .msg file เป็นคอนเทนเนอร์ Compound File Binary (CFB). CFBReader เสนอให้ การเข้าถึงต้นกํากับอ่านเท่านั้น, ทําให้คุณสามารถทํารายการเก็บและสตรีมได้ เดินทางไปยัง ระดับชั้น และสกัดไบต์สายสด โดยไม่ต้องพึ่งพา MsgReader.

from aspose.email_foss.cfb import CFBReader

with CFBReader.from_file("message.msg") as reader:
    print(f"CFB version  : {reader.major_version}")
    print(f"Sector size  : {reader.sector_size}")

    for depth, entry in reader.iter_tree():
        indent = "  " * depth
        size = entry.stream_size if entry.is_stream() else "-"
        print(f"{indent}{entry.name}  [size={size}]")

การจัดการเครื่องติดต่อ

การเพิ่มเติมตามทางเว็บไซต์นี้ MapiMessage.add_attachment() (สําหรับข้อมูลไบนารี) หรือ MapiMessage.add_embedded_message_attachment() (สําหรับข้อความ MSG ที่อยู่ในห่อ) ทั้งสองวิธีนี้ รับชื่อไฟล์, ไบต์ raw และ MIME แบบเลือก.

message = msg.MapiMessage.create(“Attachment demo”, “See files below.”) message.add_attachment(“data.csv”, b"id,value\n1,100\n", mime_type=“text/csv”)

inner = msg.MapiMessage.create(“Nested message”, “This is embedded.”) message.add_embedded_message_attachment(inner, “nested.msg”) message.save(“with_attachments.msg”)


---

```python
from aspose.email_foss import msg

message = msg.MapiMessage.create("Attachment demo", "See files below.")
message.add_attachment("data.csv", b"id,value\n1,100\n", mime_type="text/csv")

inner = msg.MapiMessage.create("Nested message", "This is embedded.")
message.add_embedded_message_attachment(inner, "nested.msg")
message.save("with_attachments.msg")

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

pip install aspose-email-foss

Create and save a minimal MSG

message = msg.MapiMessage.create(“Hello World”, “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”)

Load it back and convert to EML

with msg.MapiMessage.from_file(“hello.msg”) as loaded: print(loaded.get_property_value(msg.PropertyId.SUBJECT)) eml = loaded.to_email_bytes() open(“hello.eml”, “wb”).write(eml)


---

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

# Create and save a minimal MSG
message = msg.MapiMessage.create("Hello World", "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")

# Load it back and convert to EML
with msg.MapiMessage.from_file("hello.msg") as loaded:
    print(loaded.get_property_value(msg.PropertyId.SUBJECT))
    eml = loaded.to_email_bytes()
    open("hello.eml", "wb").write(eml)

รูปแบบที่สนับสนุน

FormatExtensionReadWrite
ไบนารีไฟล์ผสม.cfb
Outlook ข้อความ.msg
EML / RFC 5322.eml

หลักสูตรและใบอนุญาต

Aspose.Email FOSS for Python is released under the MIT License. The source repository is มีที่: github.com/aspose-email-foss และการ แพ็คเกจถูกประกาศใน PyPI เป็น aspose-email-foss.การใช้ทางการค้า, การปรับปรุงและ การกระจายใหม่ทั้งหมดถูกอนุญาตภายใต้เงื่อนไข MIT.


เริ่มต้น

แหล่งข้อมูลที่เกี่ยวข้อง