Avvio rapido

Quickstart: Crea e Leggi un file MSG

Questa guida ti porta da un nuovo installare a un file MSG funzionante in pochi passi utilizzando MapiMessage da: aspose.email_foss.


Precondizioni

RequisitiDettaglio
Python3.10 or later
pip (pipo)versione corrente
OSWindows, macOS o Linux
DipendenzeNessuna Python puro

Passo 1 Installare il pacchetto

pip install aspose-email-foss

Conferma l’ installazione:

from aspose.email_foss.msg import MapiMessage
print("aspose-email-foss is ready.")

Passo 2 Crea un file MSG

Utilizzo: MapiMessage.create() per costruire un messaggio, impostare proprietà del mittente con PropertyId, aggiungere un destinatario, allegare un file e chiamare save() per scrivere il .msg - La produzione.

from datetime import datetime, timezone
from aspose.email_foss import msg

message = msg.MapiMessage.create("Hello from Python", "This is the message body.")
message.set_property(msg.PropertyId.SENDER_NAME, "Build Agent")
message.set_property(msg.PropertyId.SENDER_EMAIL_ADDRESS, "agent@example.com")
message.set_property(
    msg.PropertyId.MESSAGE_DELIVERY_TIME,
    datetime(2026, 1, 1, 9, 0, tzinfo=timezone.utc),
)
message.add_recipient("alice@example.com", display_name="Alice Example")
message.add_attachment("readme.txt", b"Hello, world!\n", mime_type="text/plain")
message.save("quickstart.msg")
print("quickstart.msg written.")

Passo 3 Leggere il file MSG di nuovo

Caricare il file salvato con: MapiMessage.from_file() e accedere al sistema di subject e di cui al punto 2 body proprietà direttamente.

from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    print(f"Subject : {message.subject}")
    print(f"Body    : {message.body}")

Fase 4 Conversione in EML

to_email_message() restituisce un Python standard email.message.EmailMessage Oggetto. to_email_bytes() per scrivere un RFC 5322 pronto all’invio .eml - Il fascicolo.

from pathlib import Path
from aspose.email_foss import msg

with msg.MapiMessage.from_file("quickstart.msg") as message:
    Path("quickstart.eml").write_bytes(message.to_email_bytes())
    print("quickstart.eml written.")

Passi successivi

 Italiano