Johdanto
Aspose.Email FOSS for Python is now available on PyPI: a free, MIT-licensed library for creating, reading, and converting Outlook .msg tiedostot kokonaan Python:ssa, ilman riippuvuutta Microsoft Officeen tai mihinkään natiivilaajennukseen. Asenna se pip install aspose-email-foss>=26.3 ja aloita työskentely MSG- ja CFB-säiliöiden kanssa heti.
Kirjasto kohdistuu Python 3.10:een ja uudempiin versioihin. Se toteuttaa Compound File Binary (CFB) -formaatin ja MSG-viestiformaatin alusta alkaen, tarjoten deterministisen hallinnan siihen, miten sähköpostiviestit sarjoitetaan ja luetaan. Koska toteutus on puhdasta Python:aa, se toimii identtisesti Windowsissa, macOSissa, Linuxissa ja konttiympäristöissä.
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.
Keskeiset ominaisuudet
Luo MSG-tiedostoja
Rakenna Outlook-yhteensopivia .msg tiedostoja alusta alkaen käyttäen MapiMessage.create(). Aseta standardit MAPI-ominaisuudet, kuten aihe, lähettäjä, toimitusaika ja näyttökentät, läpi PropertyId enum. Lisää vastaanottajia käyttäen add_recipient() ja tiedostoliitteitä käyttäen add_attachment(), tallenna sitten tulos levylle.
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")
Lue ja muunna MSG EML:ksi
Lataa olemassa oleva .msg tiedosto käyttäen MapiMessage.from_file() ja muunna se standardiksi Python:ksi EmailMessage objektiksi käyttäen to_email_message(). Sieltä voit sarjoittaa EML-tavuiksi tallennusta tai SMTP:n kautta edelleenlähetystä varten.
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)
Tutki MSG:n sisäisiä rakenteita
Käytä MsgReader ja sen taustalla oleva CFBReader tutkia MSG-tiedoston binaarirakennetta. Pääse CFB-metatietoihin (versio, sektorikoko, hakemistomerkintöjen määrä) ja käy läpi MAPI-ominaisuuksien merkinnät binaaritasona.
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()
Alhaisen tason CFB-pääsy
Lue ja selaa mitä tahansa Compound File Binary -konttia käyttäen CFBReader. Luettele tallennustilat ja virrat, ratkaise polut nimen perusteella ja pura raakavirran data mukautettua käsittelyä varten.
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()
Pika-aloitus
Asenna kirjasto ja luo ensimmäinen MSG-tiedostosi alle kymmenellä rivillä:
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"])
Tuetut formaatit
| Muoto | Tuo | Vienti |
|---|---|---|
| MSG | Kyllä | Kyllä |
| CFB | Kyllä | Kyllä |
Avoin lähdekoodi ja lisensointi
Aspose.Email FOSS for Python is released under the MIT License. Voit käyttää sitä henkilökohtaisissa, sisäisissä ja kaupallisissa projekteissa ilman rajoituksia. Lähdekoodi on saatavilla GitHub.