Introduzione

Aspose.Email FOSS for Python is now available on PyPI: a free, MIT-licensed library for creating, reading, and converting Outlook .msg file interamente in Python, senza dipendenze da Microsoft Office o da alcuna estensione nativa. Installalo con pip install aspose-email-foss>=26.3 e inizia a lavorare con i contenitori MSG e CFB immediatamente.

La libreria è destinata a Python 3.10 e versioni successive. Implementa il formato Compound File Binary (CFB) e il formato di messaggio MSG da zero, offrendoti un controllo deterministico su come i messaggi email vengono serializzati e letti. Poiché l’implementazione è puramente Python, funziona identicamente su Windows, macOS, Linux e in ambienti containerizzati.

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.


Caratteristiche principali

Creare file MSG

Crea file compatibili con Outlook .msg da zero usando MapiMessage.create(). Imposta le proprietà MAPI standard come oggetto, mittente, ora di consegna e campi di visualizzazione tramite il PropertyId enum. Aggiungi i destinatari con add_recipient() e gli allegati di file con add_attachment(), quindi salva il risultato su disco.

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

Leggere e convertire MSG in EML

Carica un file esistente .msg con MapiMessage.from_file() e convertilo in un Python standard EmailMessage oggetto tramite to_email_message(). Da lì, serializza in byte EML per l’archiviazione o l’inoltro tramite 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)

Ispezionare l’interno di MSG

Usa MsgReader e il suo sottostante CFBReader per ispezionare la struttura binaria di un file MSG. Accedi ai metadati CFB (versione, dimensione del settore, conteggio delle voci della directory) e itera sulle voci di proprietà MAPI a livello binario.

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

Accesso CFB a basso livello

Leggi e attraversa qualsiasi contenitore Compound File Binary usando CFBReader. Elenca archivi e flussi, risolvi i percorsi per nome e estrai i dati grezzi del flusso per l’elaborazione personalizzata.

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

Avvio rapido

Installa la libreria e crea il tuo primo file MSG in meno di dieci righe:

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"])

Formati supportati

FormatoImportaEsporta
MSG
CFB

Open Source e Licenze

Aspose.Email FOSS for Python is released under the MIT License. Puoi usarlo in progetti personali, interni e commerciali senza restrizioni. Il codice sorgente è disponibile su GitHub.


Per iniziare