E‑mails laden, opslaan en converteren
Detectie van e-mailbestandsformaten
Aspose.Email API biedt de mogelijkheid om het bestandsformaat van het opgegeven berichtbestand te detecteren. De DetectFileFormat methode van FileFormatUtil klasse kan hiervoor worden gebruikt. De volgende klassen en methoden kunnen worden gebruikt om het geladen bestandsformaat te detecteren:
- Enum FileFormatType
- Klasse FileFormatInfo
- Klasse FileFormatUtil
- Methode detect_file_format(stream)
- Methode detect_file_format(file_path)
De onderstaande code‑fragment toont hoe u bestandsformaten detecteert:
from aspose.email.tools import FileFormatUtil
# Detect file format and get the detected load format
info = FileFormatUtil.detect_file_format(data_dir + "message.msg")
print("The message format is: " + str(info.file_format_type))
Bericht laden met laadopties
De onderstaande code‑fragment toont hoe u een bericht laadt met laadopties.
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
# Load Eml, html, mhtml, msg, and dat files
mail_message = MailMessage.load(data_dir + "message.eml", EmlLoadOptions())
MailMessage.load(data_dir + "description.html", HtmlLoadOptions())
MailMessage.load(data_dir + "message.mhtml", MhtmlLoadOptions())
MailMessage.load(data_dir + "message.msg", MsgLoadOptions())
# Loading with custom options
eml_load_options = EmlLoadOptions()
eml_load_options.preferred_text_encoding = "utf-8"
eml_load_options.preserve_tnef_attachments = True
MailMessage.load(data_dir + "description.html", eml_load_options)
html_load_options = HtmlLoadOptions()
html_load_options.preferred_text_encoding = "utf-8"
html_load_options.should_add_plain_text_view = True
html_load_options.path_to_resources = data_dir
MailMessage.load(data_dir + "description.html", html_load_options)
Behouden van ingesloten berichtformaat tijdens laden
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
from aspose.email.tools import FileFormatUtil
eml_load_options = EmlLoadOptions()
eml_load_options.preserve_embedded_message_format = True
mail = MailMessage.load(data_dir + "message.eml", eml_load_options)
file_format = FileFormatUtil.detect_file_format(mail.attachments[0].content_stream).file_format_type
print("Embedded message file format: " + str(file_format))
E‑mails opslaan en converteren
Aspose.Email maakt het eenvoudig om elk berichttype naar een ander formaat te converteren. Om deze functie te demonstreren, laadt de code in dit artikel drie soorten berichten van de schijf en slaat ze op in andere formaten. De basisklasse SaveOptions en de klassen EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions kan worden gebruikt voor extra instellingen bij het opslaan van berichten naar andere formaten. Het artikel toont hoe u deze klassen gebruikt om een voorbeeld-e‑mail op te slaan als:
- EML‑formaat.
- Outlook MSG.
- MHTML‑formaat.
- HTML‑formaat.
Opslaan als EML
De onderstaande code‑fragment toont hoe u een EML‑bericht laadt en opslaat op de schijf in hetzelfde formaat.
from aspose.email import MailMessage, SaveOptions
# Initialize and Load an existing EML file by specifying the MessageFormat
mail_message = MailMessage.load(data_dir + "message.eml")
mail_message.save(data_dir + "LoadAndSaveFileAsEML_out.eml", SaveOptions.default_eml)
Boundaries behouden in EML‑bestanden
De onderstaande code‑fragment toont hoe u een EML‑bestand laadt en opslaat in hetzelfde formaat met behoud van de oorspronkelijke boundaries.
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved original boundaries
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
mail_message.save(data_dir + "PreserveOriginalBoundaries_out.eml", eml_save_options)
MIME‑boundary‑strings aanpassen
Aspose.Email voor .NET stelt u in staat om de boundary‑sjabloon te configureren die in MIME‑berichten wordt gebruikt. Dit wordt bereikt via de boundaries_template eigenschap van de EmlSaveOptions klasse.
Door een aangepaste sjabloon in te stellen, kunt u definiëren hoe MIME‑boundaries worden gegenereerd. Het {#}-wildcard in de sjabloon wordt vervangen door een oplopend boundary‑nummer, waardoor dynamische en leesbare boundary‑strings ontstaan.
Als boundaries_template is niet ingesteld (standaard is None), gebruikt de bibliotheek zijn interne boundary‑formaat.
Het onderstaande codevoorbeeld toont hoe u de MIME‑boundary‑strings kunt aanpassen bij het opslaan van een e‑mailbericht in .eml‑formaat:
import aspose.email as ae
# Create a sample MailMessage
message = ae.MailMessage()
message.subject = "Custom MIME Boundary"
message.body = "This message uses a custom MIME boundary template."
message.to.append(ae.MailAddress("recipient@example.com"))
# Configure save options with a custom boundary template
save_options = ae.EmlSaveOptions(ae.MailMessageSaveType.EML_FORMAT)
save_options.boundaries_template = "boundary--{#}"
# Save the message to .eml with custom boundaries
message.save("Custom_Boundary_Message.eml", save_options)
Hier is een voorbeeld van de berichtstructuur met aangepaste grenzen opgeslagen met de bovenstaande code:
From: sender@example.com
To: recipient@example.com
Subject: Custom_Boundary_Message
Date: Fri, 27 Dec 2024 12:00:00 +0000
Content-Type: multipart/mixed;
boundary="boundary--1"
This is a multi-part message in MIME format.
--boundary--1
Content-Type: multipart/alternative; boundary="boundary--2"
--boundary--2
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"
This is the plain text content of the email.
--boundary--2
Content-Type: text/html; charset="windows-1251"
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<p>This is the <b>HTML</b> content of the email.</p>
</body>
</html>
--boundary--2--
--boundary--1
Content-Type: application/octet-stream; name="report-2023-08.xlsx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report-2023-08.xlsx"
UEsDBBQABgAIAAAAIQBi7...
--boundary--1--
TNEF‑bijlagen behouden in EML‑bestanden
De onderstaande code‑fragment toont hoe u een EML‑bestand opslaat met behoud van TNEF‑bijlagen.
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType, FileCompatibilityMode
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved attachment
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
eml_save_options.file_compatibility_mode = FileCompatibilityMode.PRESERVE_TNEF_ATTACHMENTS
mail_message.save(data_dir + "PreserveTNEFAttachment_out.eml", eml_save_options)
Opslaan als MSG
De onderstaande code‑fragment toont hoe u een EML‑bericht laadt en converteert naar MSG met de juiste optie uit SaveOptions.
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save the Email message to disk in ASCII format and Unicode format
eml.save(data_dir + "AnEmail_out.msg", SaveOptions.default_msg_unicode)
Data behouden in MSG‑bestanden
De MsgSaveOptions class maakt het mogelijk het bronbericht op te slaan als een Outlook‑Message‑bestand (MSG) met behoud van data. De onderstaande code‑fragment toont hoe u een EML‑bestand opslaat als MSG en data behoudt.
from aspose.email import MailMessage, MsgSaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save as msg with preserved dates
msg_save_options = MsgSaveOptions(MailMessageSaveType.outlook_message_format_unicode)
msg_save_options.preserve_original_dates = True
eml.save(data_dir + "outTest_out.msg", msg_save_options)
Opslaan als MHTML
Verschillende MHTML‑opties kunnen worden gebruikt om het gewenste resultaat te bereiken. Het volgende codefragment toont hoe je een EML‑bericht laadt in MailMessage en converteer het naar MHTML.
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
eml.save(data_dir + "AnEmail_out.mhtml", SaveOptions.default_mhtml)
Aangepaste MHTML‑conversie‑instellingen
De MhtSaveOptions klasse biedt extra opties voor het opslaan van e‑mailberichten in MHTML‑formaat. De enumerator MhtFormatOptions maakt het mogelijk om extra e‑mailinformatie naar het uitvoer‑MHTML te schrijven. De volgende extra velden kunnen worden geschreven:
- NONE - Er zijn geen specifieke instellingen gespecificeerd.
- WRITE_HEADER - Geeft aan dat header‑informatie moet worden geschreven.
- WRITE_OUTLINE_ATTACHMENTS - Geeft aan dat outline‑bijlagen moeten worden geschreven.
- WRITE_COMPLETE_EMAIL_ADDRESS - Geeft aan dat het volledige e‑mailadres moet worden geschreven in alle e‑mailheaders.
- NO_ENCODE_CHARACTERS - Geeft aan dat er geen overdrachts‑codering van tekens gebruikt mag worden.
- HIDE_EXTRA_PRINT_HEADER - Geeft aan dat de PageHeader onzichtbaar zal zijn.
- WRITE_COMPLETE_TO_EMAIL_ADDRESS - Geeft aan dat het volledige e‑mailadres moet worden geschreven in de ‘To’-header.
- WRITE_COMPLETE_FROM_EMAIL_ADDRESS - Geeft aan dat het volledige e‑mailadres moet worden geschreven in de ‘From’-header.
- WRITE_COMPLETE_CC_EMAIL_ADDRESS - Geeft aan dat het volledige e‑mailadres moet worden geschreven in de ‘Cc’-header.
- WRITE_COMPLETE_BCC_EMAIL_ADDRESS - Geeft aan dat het volledige e‑mailadres moet worden geschreven in de ‘Bcc’-header.
- RENDER_CALENDAR_EVENT - Geeft aan dat tekst van een agenda‑event moet worden geschreven in de uitvoer‑mhtml.
- SKIP_BYTE_ORDER_MARK_IN_BODY - Geeft aan dat Byte Order Mark (BOM)-bytes naar de body moeten worden geschreven.
- RENDER_V_CARD_INFO - Geeft aan dat tekst uit VCard AlternativeView moet worden geschreven in de uitvoer‑mhtml.
- DISPLAY_AS_OUTLOOK - Geeft aan dat de From‑header wordt weergegeven zoals in Outlook.
- RENDER_TASK_FIELDS - Geeft aan dat de specifieke taakvelden moeten worden geschreven in de uitvoer‑mhtml.
De onderstaande code‑fragment toont hoe u een EML‑bestand naar MHTML converteert met optionele instellingen.
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MailMessageSaveType, FileCompatibilityMode
# Load an existing EML file
eml = MailMessage.load(data_dir + "message.eml")
# Save as mht with header
mht_save_options = MhtSaveOptions()
# Specify formatting options required
# Here we are specifying to write header information to output without writing extra print header
# and the output headers should be displayed as the original headers in the message
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.HIDE_EXTRA_PRINT_HEADER | MhtFormatOptions.DISPLAY_AS_OUTLOOK
# Check the body encoding for validity.
mht_save_options.check_body_content_encoding = True
eml.save(data_dir + "outMessage_out.mht", mht_save_options)
Agenda‑evenementen opnemen in MHTML‑conversie
De MhtFormatOptions.RenderCalendarEvent rendert de agenda‑evenementen naar de output‑MHTML. De onderstaande code‑fragment toont hoe u agenda‑evenementen rendert tijdens conversie naar MHTML:
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
file_name = "message.msg"
# Load the MSG file
msg = MailMessage.load(data_dir + file_name)
# Create MHT save options
options = MhtSaveOptions()
options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.RENDER_CALENDAR_EVENT
# Save the message as MHTML
msg.save(data_dir + "Meeting with Recurring Occurrences.mhtml", options)
E‑mail exporteren naar MHT zonder inline‑afbeeldingen
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Create MHT save options
mht_save_options = MhtSaveOptions()
mht_save_options.skip_inline_images = True
# Save the message as MHTML without inline images
eml.save(data_dir + "EmlToMhtmlWithoutInlineImages_out.mht", mht_save_options)
E‑mail exporteren naar MHT met aangepaste tijdzone
MailMessage class biedt de TimeZoneOffset eigenschap om een aangepaste tijdzone in te stellen bij het exporteren naar MHT. De onderstaande code‑fragment toont hoe u een e‑mail exporteert naar MHT met een aangepaste tijdzone:
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
from datetime import timedelta
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Set the local time for message date
eml.time_zone_offset = timedelta(hours=-8)
# The dates will be rendered by the local system time zone
mht_save_options = MhtSaveOptions()
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER
eml.save(data_dir + "ExportEmailToMHTWithCustomTimezone_out.mhtml", mht_save_options)
Opslaan als HTML
De HtmlSaveOptions class maakt het mogelijk de berichtinhoud naar HTML te exporteren met de optie om ingesloten bronnen op te slaan. De onderstaande code‑fragment toont hoe u een bericht opslaat als HTML waarbij de standaardwaarde van embed_resources true is.
from aspose.email import MailMessage, SaveOptions, HtmlSaveOptions, HtmlFormatOptions
# Load the EML file
message = MailMessage.load(data_dir + "message.eml")
# Save the Email message as HTML
message.save(data_dir + "SaveAsHTML_out.html", SaveOptions.default_html)
# OR
eml = MailMessage.load(data_dir + "message.eml")
options = HtmlSaveOptions()
options.embed_resources = False
options.html_format_options = (
HtmlFormatOptions.WRITE_HEADER
| HtmlFormatOptions.WRITE_COMPLETE_EMAIL_ADDRESS
) # save the message headers to output HTML using the formatting options
eml.save(data_dir + "SaveAsHTML1_out.html", options)
Opslaan als Outlook‑sjabloon (.OFT)
De onderstaande code‑fragment toont hoe u een bericht opslaat als een Outlook‑sjabloon (.oft) bestand.
from aspose.email import MailMessage, SaveOptions
eml = MailMessage("test@from.to", "test@to.to", "template subject", "Template body")
oft_eml_file_name = "EmlAsOft_out.oft"
options = SaveOptions.default_msg_unicode
options.save_as_template = True
eml.save(data_dir + oft_eml_file_name, options)