Caricamento e Salvataggio del Messaggio
Rilevamento dei Formati dei File
L’API Aspose.Email fornisce la capacità di rilevare il formato del file del messaggio fornito. Il metodo DetectFileFormat della classe FileFormatUtil può essere utilizzato per ottenere questo risultato. Le seguenti classi e metodi possono essere usati per rilevare il formato del file caricato.
- Enum FileFormatType
- Classe FileFormatInfo
- Classe FileFormatUtil
- Metodo detect_file_format(stream)
- Metodo detect_file_format(file_path)
Il seguente frammento di codice mostra come rilevare i formati dei file.
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))
Caricamento di un messaggio con opzioni di caricamento
Il seguente frammento di codice mostra come caricare un messaggio con le opzioni di caricamento.
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)
Preservare il formato dei messaggi incorporati durante il caricamento
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))
Salvataggio e Conversione dei Messaggi
Aspose.Email rende facile convertire qualsiasi tipo di messaggio in un altro formato. Per dimostrare questa funzionalità, il codice in questo articolo carica tre tipi di messaggi dal disco e li salva nuovamente in altri formati. La classe base SaveOptions e le classi EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions per impostazioni aggiuntive durante il salvataggio MailMessage può essere usato per salvare i messaggi in altri formati. L’articolo mostra come usare queste classi per salvare una email di esempio come:
- Formato EML.
- Outlook MSG.
- Formato MHTML.
- Formato HTML.
Caricamento EML e Salvataggio come EML
Il seguente frammento di codice mostra come caricare un messaggio EML e salvarlo su disco nello stesso formato.
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)
Caricamento EML e Salvataggio come EML Preservando i Confini Originali
Il seguente frammento di codice mostra come caricare un EML e salvarlo come EML preservando i confini originali.
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)
Salvataggio come EML Preservando gli Allegati TNEF
Il seguente frammento di codice mostra come salvare come EML preservando gli allegati TNEF.
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)
Caricamento EML, Salvataggio in MSG
Il frammento di codice seguente ti mostra come caricare un messaggio EML e convertirlo in MSG usando l’opzione appropriata da 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)
Salvataggio come MSG con Date Conservate
Il MsgSaveOptions la classe permette di salvare il messaggio sorgente come file Outlook Message (MSG) preservando le date. Il frammento di codice seguente ti mostra come salvare come MSG con Date Preservate.
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)
Salvataggio di MailMessage come MHTML
Diverse opzioni di MHTML possono essere usate per ottenere i risultati desiderati. Il seguente frammento di codice mostra come caricare un messaggio EML inMailMessage e lo converte in 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)
Conversione in MHTML con Impostazioni Opzionali
Il MhtSaveOptions la classe fornisce opzioni aggiuntive per salvare i messaggi email in formato MHTML. L’enumeratore MhtFormatOptions consente di scrivere informazioni email aggiuntive nel MHTML di output. I seguenti campi aggiuntivi possono essere scritti:
- NONE - Nessuna impostazione specifica è specificata.
- WRITE_HEADER - Indica che le informazioni dell’intestazione dovrebbero essere scritte.
- WRITE_OUTLINE_ATTACHMENTS - Indica che gli allegati outline dovrebbero essere scritti.
- WRITE_COMPLETE_EMAIL_ADDRESS - Indica che l’indirizzo email completo dovrebbe essere scritto in tutte le intestazioni email.
- NO_ENCODE_CHARACTERS - Indica che non dovrebbe essere usata alcuna codifica di trasferimento dei caratteri.
- HIDE_EXTRA_PRINT_HEADER - Indica che PageHeader sarà invisibile.
- WRITE_COMPLETE_TO_EMAIL_ADDRESS - Indica che l’indirizzo email completo dovrebbe essere scritto nell’intestazione ‘To’.
- WRITE_COMPLETE_FROM_EMAIL_ADDRESS - Indica che l’indirizzo email completo dovrebbe essere scritto nell’intestazione ‘From’.
- WRITE_COMPLETE_CC_EMAIL_ADDRESS - Indica che l’indirizzo email completo dovrebbe essere scritto nell’intestazione ‘Cc’.
- WRITE_COMPLETE_BCC_EMAIL_ADDRESS - Indica che l’indirizzo email completo dovrebbe essere scritto nell’intestazione ‘Bcc’.
- RENDER_CALENDAR_EVENT - Indica che il testo dell’evento del calendario dovrebbe essere scritto nell’mhtml di output.
- SKIP_BYTE_ORDER_MARK_IN_BODY - Indica che i byte Byte Order Mark (BOM) dovrebbero essere scritti nel corpo.
- RENDER_V_CARD_INFO - Indica che il testo da VCard AlternativeView dovrebbe essere scritto nell’mhtml di output.
- DISPLAY_AS_OUTLOOK - Indica che l’intestazione From sarà visualizzata come in Outlook.
- RENDER_TASK_FIELDS - Indica che i campi specifici del Task dovrebbero essere scritti nell’mhtml di output.
Il seguente frammento di codice mostra come convertire un file EML in MHTML con impostazioni opzionali.
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)
Renderizzazione degli Eventi del Calendario durante la Conversione in MHTML
Il MhtFormatOptions.RenderCalendarEvent renderizza gli eventi del Calendario nell’output MHTML. Il seguente frammento di codice mostra come renderizzare gli eventi del calendario durante la conversione in 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)
Esportazione dell’email in MHT senza immagini in linea
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)
Esportazione Email in MHT con Fuso Orario Personalizzato
MailMessage classe fornisce la proprietà TimeZoneOffset per impostare il fuso orario personalizzato durante l’esportazione in MHT. Il seguente frammento di codice mostra come esportare un’email in MHT con fuso orario personalizzato.
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)
Esportare email in EML
Il seguente frammento di codice mostra come esportare l’email in EML.
from aspose.email import MailMessage, SaveOptions
# Load the EML file
msg = MailMessage.load(data_dir + "message.eml")
# Save the Email message as EML
msg.save(data_dir + "ExporttoEml_out.eml", SaveOptions.default_eml)
Salvataggio del Messaggio come HTML
Il HtmlSaveOptions classe ti consente di esportare il corpo del messaggio in HTML con l’opzione di salvare le risorse incorporate. Il seguente frammento di codice mostra come salvare il messaggio come HTML dove il valore predefinito di embed_resources è true.
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)
Salvataggio del Messaggio come Modello Outlook (.oft)
Il seguente frammento di codice mostra come salvare il messaggio come file modello Outlook (.oft).
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)