Inici ràpid

Quickstart: Crear i llegir un fitxer MSG

Aquesta guia et porta d’una instal·lació fresca a un fitxer MSG en uns pocs passos utilitzant MapiMessage de la aspose.email_foss.


Condicions previes

RequisitsDetall
Python3.10 or later
pip (p)Versió actual
OSWindows, macOS o Linux
DependènciesCap Python pur

Pas 1 Instal·lar el paquet

pip install aspose-email-foss

Confirma la instal·lació:

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

Pas 2 Crear un fitxer MSG

Utilització: MapiMessage.create() per construir un missatge, fixar propietats del remitedor amb PropertyId, afegir un destinatari, adjuntar un fitxer i trucar save() per escriure el .msg sortida.

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

Pas 3 Llegir el fitxer MSG de nou

Carregar el fitxer salvat amb: MapiMessage.from_file() i accedir al subject i de la body les propietats directament.

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

Pas 4 Conversió a EML

to_email_message() Retorna un Python estàndard email.message.EmailMessage object. Utilitzar to_email_bytes() per escriure un RFC 5322 a punt d’enviar .eml Arxiu.

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

Primiers passos

 Català