Loading and Saving Messages

Loading and Saving Email Messages

Detect a File Format

Aspose.Email API provides the capability to detect the file format of the provided message file. The DetectFileFormat method of the FileFormatUtil class can be used to achieve this. The following classes and methods can be used to detect the loaded file format.

The following code snippet shows you how to detect file formats.

Load an Email Message

To load a message with specific load options, Aspose.Email provides the LoadOptions class that can be used as follow:

Preserve Embedded Message Format during Loading

Save and Convert Email Messages

Aspose.Email makes it easy to convert any message type to another format. To demonstrate this feature, the code in this article loads three types of messages from disk and saves them back in other formats. The base class SaveOptions and the classes EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions for additional settings when saving MailMessage can be used for saving messages to other formats. The article shows how to use these classes to save a sample email as:

  • EML format.
  • Outlook MSG.
  • MHTML format.
  • HTML format.

Load and Save an Email Message

The following code snippet shows you how to load an EML message and saves it to the disc in the same format.

Load and Save an Email Message Preserving the Original Boundaries

The following code snippet shows you how to load EML and save as EML preserving the original boundaries.

Saving as EML Preserving TNEF Attachments

The following code snippet shows you how to save as EML preserving TNEF attachments.

Save EML to MSG

The following code snippet shows you how to loads an EML message and converts it to MSG using the appropriate option from SaveOptions.

Save EML to MSG with Preserved Dates

The MsgSaveOptions class allows you to save the source message as an Outlook Message file (MSG) preserving dates. The following code snippet shows you how to Saving as MSG with Preserved Dates.


Different options of MHTML can be used to obtain the desired results. The following code snippet shows you how to load an EML message into MailMessage and convert it to MHTML with a message date in the UTC system.

// Set options for MHTML output
MhtSaveOptions saveOptions = SaveOptions.getDefaultMhtml();
// save a message date as UTC date

// Initialize and load an existing EML file
try (MailMessage mailMessage = MailMessage.load(dataDir + "Message.eml")) {
    mailMessage.save(outDir + "Message_out.mhtml", saveOptions);

Format MHT Headers Globally when Saving from EML

With Aspose.Email, you can use the global formatting options for the Mht header. The global options set the common formatting of an Mht header for all MhtSaveOptions instances. This feature is designed to avoid setting formatting for each instance of MhtSaveOptions.

Use the following methods of the GlobalFormattingOptions class and the code sample below to set the formatting of an Mht header:

// saveOptions1 and saveOptions2 have the same mht header formatting
MhtSaveOptions saveOptions1 = new MhtSaveOptions();
MhtSaveOptions saveOptions2 = new MhtSaveOptions();

Convert EML to MHTML with Optional Settings

The MhtSaveOptions class provides additional options for saving email messages to MHTML format. The enumerator MhtFormatOptions makes it possible to write additional email information to the output MHTML. The following additional fields can be written:

  • WriteHeader - write the email header to the output file.
  • WriteOutlineAttachments - write outline attachments to the output file.
  • WriteCompleteEmailAddress - write the complete email address to the output file.
  • NoEncodeCharacters - no transfer encoding of characters should be used.
  • HideExtraPrintHeader - hide extra print header from the top of the output file.
  • WriteCompleteToEmailAddress - write the complete recipient email address to the output file.
  • WriteCompleteFromEmailAddress - write the complete sender email address to the output file.
  • WriteCompleteCcEmailAddress - write the complete email addresses of any carbon-copied recipients to the output file.
  • WriteCompleteBccEmailAddress - write the complete email address of any blind carbon-copied recipients to the output file.
  • RenderCalendarEvent - write text from the calendar event to the output file.
  • SkipByteOrderMarkInBody - write Byte Order Mark(BOM) bytes to the output file.
  • RenderVCardInfo -write text from VCard AlternativeView to the output file.
  • DisplayAsOutlook - display From header.
  • RenderTaskFields - writes specific Task fields to the output file.
  • None - No setting specified.

The following code snippet shows you how to convert EML files to MHTML with optional settings.

Render Calendar Events while Converting to MHTML

The MhtFormatOptions.RenderCalendarEvent renders the Calendar events to the output MTHML.The following code snippet shows you how to render calendar events while converting to MHTML.

Export Email to MHT without Inline Images

Export Email to MHT with Customized TimeZone

MailMessage class provides the setTimeZoneOffset property to set customized Timezone while exporting to MHT. The following code snippet shows you how to export email to MHT with customized TimeZone.

MailMessage msg = MailMessage.load(filename, new MsgLoadOptions());
msg.setDate(new Date());

// Set the timezone offset in milliseconds

MhtSaveOptions mhtOptions = new MhtSaveOptions();
msg.save(dataDir + "ExportToMHTWithCustomTimezone_out.mhtml", mhtOptions);

Exporting Email to EML

The following code snippet shows you how to export emails to EML.

Save Email as HTML

The HtmlSaveOptions class allows you to export the message body to HTML. The following code snippet shows you how to save a message as HTML.

Preserve Custom Icons in a Message while Converting to HTML

Sometimes, the message contains in-line attachments, that are displayed as icon images in a message body. Such messages may create problems while converting them to HTML, since the icon images are lost. This is because attachment’s icons are not held directly in the message.

The user of the Aspose.Email can customize the icons for attachments when converting the message to HTML. For that, the HtmlSaveOptions.ResourceHtmlRendering event is used to customize the rendering of resource files (such as attachments) when saving an email message as an HTML file. In the code sample below, the event handler is used to dynamically set the path to resource files (icons) based on the content type of the attachment. This allows for customized rendering of resources in the HTML output based on their file type.

 HtmlSaveOptions options = new HtmlSaveOptions();

options.setResourceHtmlRenderingHandler(new ResourceHtmlRenderingHandler() {


   public void invoke(Object sender, ResourceHtmlRenderingEventArgs e) {

        AttachmentBase attachment = (AttachmentBase) sender;


       if (attachment.getContentType().getName().endsWith(".pdf")) {


       } else if (attachment.getContentType().getName().endsWith(".docx")) {


       } else if (attachment.getContentType().getName().endsWith(".jpg")) {


       } else {






String fileName = "message.msg";

MailMessage mailMessage = MailMessage.load(fileName);

mailMessage.save("fileName.html", options);

Set Time and Timezone when Saving EML as HTML

Aspose.Email users can set the time and timezone display formats in HtmlSaveOptions. The HeadersFormattingOptions class allows to specify headers formatting options when saving MailMessage to Mhtml or Html format. The following methods of the HtmlFormatOptions class specify the fields to be displayed in the output file:

  • RenderCalendarEvent - Indicates that text from calendar event should be written in output mhtml.
  • RenderVCardInfo - Indicates that text from VCard AlternativeView should be written in output html.

The following code sample shows how to set the time and timezone when saving EML to HTML:

MailMessage msg = MailMessage.load("fileName");
HtmlSaveOptions options = new HtmlSaveOptions();
options.getFormatTemplates().set_Item("DateTime", "MM d yyyy HH:mm tt");

Saving as HTML without Embedding Resources

Saving a Message as an Outlook Template (.oft) file

The following code snippet shows you how to save a message as an outlook template (.oft) file.