บทนำ
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 คุณสามารถเดินทางผ่านโครงสร้างการจัดเก็บ, ดึงข้อมูลสตรีมดิบ, สร้างคอนเทนเนอร์ใหม่ตั้งแต่ต้น, และทำการซีเรียลไลซ์กลับเป็นไบต์ ทั้งสอง CFBError และ MsgError ให้การจัดการข้อยกเว้นแบบโครงสร้างสำหรับอินพุตที่ผิดรูปแบบ.
โพสต์นี้จะพาคุณผ่านแต่ละความสามารถพร้อมตัวอย่างโค้ดที่ตรวจสอบแล้วซึ่งดึงมาจากชุดทดสอบของไลบรารีและสคริปต์ตัวอย่างโดยตรง.
สิ่งที่รวมอยู่
การเข้าถึงคุณสมบัติ MAPI
ไฟล์ MSG ทุกไฟล์ถูกสร้างบนคุณสมบัติของ MAPI. MapiMessage เปิดเผย set_property() และ get_property() เพื่อทำงานกับคุณสมบัติโดยใช้ PropertyId ค่าของ enum. ใช้ 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(), ระบุชื่อที่แสดงและประเภทผู้รับ (ถึง, 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’s 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
สร้างและซีเรียลไลซ์คอนเทนเนอร์ Compound File Binary อย่างอิสระจากชั้น MSG. CFBDocument.from_file() โหลดคอนเทนเนอร์ที่มีอยู่เข้าสู่โมเดลเอกสารที่เปลี่ยนแปลงได้พร้อมกับ 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 License. คุณสามารถใช้มันในโครงการส่วนบุคคล ภายใน และเชิงพาณิชย์ได้โดยไม่มีข้อจำกัด โค้ดต้นฉบับพร้อมให้ใช้งานบน GitHub.