Hoe MSG-bestanden te maken in Python
aspose-email-foss voor Python stelt je in staat om Outlook MSG‑bestanden programmatisch te maken zonder Microsoft Office.
Stapsgewijze handleiding
Stap 1: Installeer het pakket
pip install aspose-email-fossStap 2: Maak een bericht
from aspose.email_foss.msg import MapiMessage, RECIPIENT_TYPE_CC
msg = MapiMessage.create("Meeting Notes", "Please review the attached notes.")Stap 3: Ontvangers toevoegen
msg.add_recipient("alice@example.com", display_name="Alice Smith")
msg.add_recipient("bob@example.com", display_name="Bob Jones", recipient_type=RECIPIENT_TYPE_CC)Gebruik de integer‑constanten RECIPIENT_TYPE_TO (1), RECIPIENT_TYPE_CC (2) of RECIPIENT_TYPE_BCC (3) uit aspose.email_foss.msg.
Stap 4: Bijlagen toevoegen
with open("notes.pdf", "rb") as f:
msg.add_attachment("notes.pdf", f.read(), mime_type="application/pdf")Om een ander MSG‑bericht in te sluiten:
inner = MapiMessage.create("Forwarded", "Original message body")
msg.add_embedded_message_attachment(inner, filename="forwarded.msg", mime_type="application/vnd.ms-outlook")Stap 5: Sla het MSG‑bestand op
msg.save("output.msg")Of haal bytes voor streaming:
data = msg.to_bytes()Veelvoorkomende problemen en oplossingen
Lege ontvangers in opgeslagen bestand
Zorg ervoor dat je add_recipient() aanroept vóór save(). Ontvangers worden geschreven tijdens serialisatie.
Bijlage wordt niet weergegeven in Outlook
Controleer of het MIME-type correct is. Gebruik "application/octet-stream" als fallback voor onbekende bestandstypen.
Veelgestelde vragen (FAQ)
Kan ik HTML-body instellen?
Ja. Gebruik msg.body_html = "value" om de HTML-body na creatie in te stellen.
Kan ik de MSG naar EML‑formaat converteren?
Ja. Roep msg.to_email_string() aan om de RFC 5322‑representatie te krijgen.
Maximale berichtgrootte?
Er is geen harde limiet. CFB v3 ondersteunt bestanden tot 2 GB; v4 ondersteunt grotere bestanden.