Loading and Saving Message

Loading and Saving Messages

Detecting File Formats

Aspose.Email API provides the capability to detect the file format of the provided message file. The DetectFileFormat method of 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.

Loading a Message with Load Options

The following code snippet shows you how to load a message with load options.

Preserving Embedded Message Format during Loading

Loading a Message Preserving or Removing a Signature

Signature preservation is supported by default when loading EML files. To remove the signature, you can set the LoadOptions.RemoveSignature property to true.

The code sample below shows how to remove a signature when loading a message:

var msg = MapiMessage.Load(fileName, new EmlLoadOptions() { RemoveSignature = true});

Checking Secure Emails Signature

The SecureEmailManager class allows you to check the signature of secure MailMessage objects.

The SmimeResult class stores the results of the check.

The following methods of the SecureEmailManager class and a code snippet will enable you to process a signature:

var eml = MailMessage.Load(fileName);
var result = new SecureEmailManager().CheckSignature(eml);

var certFileName = "cert.pfx";
var cert = new X509Certificate2(certFileName, "pass");
var eml = MailMessage.Load(fileName);
var store = new X509Store();
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();

var result = new SecureEmailManager().CheckSignature(eml, cert, store);

Saving and Converting 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 EML 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 EML 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 as MSG

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

Saving as 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 save as MSG with Preserved Dates.

Saving MailMessage as MHTML

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.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET

// Set options for MHTML output
MhtSaveOptions saveOptions = SaveOptions.DefaultMhtml;
saveOptions.PreserveOriginalDate = false; // save a message date as UTC date

// Initialize and load an existing EML file
using (MailMessage mailMessage = MailMessage.Load(folderPath + "Message.eml"))
{
    mailMessage.Save(folderPath + "Message_out.mhtml", saveOptions);
}

Converting 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.

Rendering 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.

Exporting Email to MHT without Inline Images

Exporting Email to MHT with customized TimeZone

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

Changing Font while Converting to MHT

Preserving RTF body when converting MSG to EML

The convertion of a MSG file to EML preserving RTF body can be done in two ways:

Both properties get or set a value indicating whether to keep the rtf body in MailMessage.

The following code snippets show how to convert a MSG file to EML and preserve RTF body:

var loadOptions = new MsgLoadOptions
{
    PreserveRtfContent = true
};

var eml = MailMessage.Load("my.msg", loadOptions);
var conversionOptions = new MailConversionOptions
{
    PreserveRtfContent = true
};

var msg = MapiMessage.Load("my.msg");

var eml = msg.ToMailMessage(conversionOptions);

Exporting Email to EML

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

Saving Message 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.

Saving as HTML without Embedding Resources

Saving Message as Outlook Template (.oft) file

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