Introduction
Aspose.Email FOSS for Python is a free, MIT-licensed library for creating, reading, and chuyển đổi tệp tin nhắn tương thích với Outlook sang Python tinh khiết. có sẵn trên PyPI như: aspose-email-foss, nó hoạt động trên Windows, macOS, Linux, Docker và môi trường không có máy chủ Không có sự phụ thuộc Microsoft Office và không có thời gian chạy sở hữu.
Thư viện bao gồm hai lĩnh vực chính: một lớp MAPI cấp cao (MapiMessage(để làm việc) với các chủ đề, cơ quan, người nhận và phụ kiện; và một cấu trúc cấp thấp Đồ chơi (MsgReader, CFBReader, CFBStorage, CFBStream(c) Phân tích các hợp chất nguyên liệu File containers binary mà hỗ trợ mỗi file .msg file .
Bài viết này đi qua các tính năng chính được gửi trong phiên bản 26.3, dựa trên bề mặt API đã xác nhận.
Các tính năng chính
Tạo tin nhắn cấp cao với MapiMessage
MapiMessage là điểm đầu tiên để tạo tin nhắn tương thích với Outlook từ Lời bài hát: Call MapiMessage.create() với một chủ đề và cơ thể, thêm người nhận và Các phụ kiện, sau đó lưu trực tiếp đến một .msg file .
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")
Đọc và kiểm tra các tập tin MSG
MsgReader cung cấp quyền truy cập có cấu trúc cho một .msg file mà không tải đầy đủ vào A MapiMessage.Nó tiết lộ các tài sản cấp cao, lưu trữ người nhận và các phụ kiện lưu trữ riêng lẻ, làm cho nó thực tế cho kiểm tra tệp lớn và Độ bền của pipelines.
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 Round-Trip: tải, sửa đổi, tiết kiệm
Because MapiMessage Hỗ trợ from_file() cho tải và save() viết, một đầy đủ chu kỳ đọc-thay đổi-tự viết chỉ cần một vài dòng. thuộc tính được đặt tên, chữ ký được in và Unicode string xử lý có sẵn thông qua set_property, PropertyId, và 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 chuyển đổi
MapiMessage.to_email_message() Chuyển đổi một MSG tải xuống thành Python tiêu chuẩn email.message.EmailMessage - Khả năng xuất khẩu vào RFC 5322 .eml Format sử dụng - Đánh giá về hướng ngược (MapiMessage.from_email_message()* Xây dựng A MapiMessage Từ một 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 Container Giao thông
Every .msg File là một container Compound File Binary (CFB). CFBReader cung cấp chỉ đọc truy cập vào cây thư mục, cho phép bạn liệt kê lưu trữ và dòng chảy, đi bộ qua hiện, và rút các byte dòng nguyên — mà không dựa vào 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}]")
Hợp tác xử lý
Các phụ kiện được thêm qua MapiMessage.add_attachment() (với dữ liệu nhị phân) hoặc MapiMessage.add_embedded_message_attachment() (đối với các thông điệp MSG) Cả hai phương pháp chấp nhận một tên file, byte nguyên chất và loại MIME tùy chọn.
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")
Khởi động nhanh
pip install aspose-email-fossCreate 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)
Các định dạng hỗ trợ
| Format | Extension | Read | Write |
|---|---|---|---|
| File Binary kết hợp | .cfb | ✓ | ✓ |
| Outlook Thông điệp | .msg | ✓ | ✓ |
| EML / RFC 5322 | .eml | ✓ | ✓ |
Open Source & Giấy phép
Aspose.Email FOSS for Python is released under the MIT License. The source repository is Có sẵn tại github.com/tạm dịch: email-foss Và The gói được công bố trên PyPI như: aspose-email-foss.- Sử dụng thương mại, sửa đổi và Việc phân phối lại được phép theo các điều khoản của MIT.