Loading, Saving and Converting different Email Message formats in C++

Loading a Message with Load Options

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

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
// Load Eml, html, mhtml, msg and dat file
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Message.eml", System::MakeObject<EmlLoadOptions>());
MailMessage::Load(dataDir + L"description.html", System::MakeObject<HtmlLoadOptions>());
MailMessage::Load(dataDir + L"Message.mhtml", System::MakeObject<MhtmlLoadOptions>());
MailMessage::Load(dataDir + L"Message.msg", System::MakeObject<MsgLoadOptions>());
// loading with custom options
System::SharedPtr<EmlLoadOptions> emlLoadOptions = System::MakeObject<EmlLoadOptions>();
emlLoadOptions->set_PrefferedTextEncoding(System::Text::Encoding::get_UTF8());
emlLoadOptions->set_PreserveTnefAttachments(true);
MailMessage::Load(dataDir + L"description.html", emlLoadOptions);
System::SharedPtr<HtmlLoadOptions> htmlLoadOptions = System::MakeObject<HtmlLoadOptions>();
htmlLoadOptions->set_PrefferedTextEncoding(System::Text::Encoding::get_UTF8());
htmlLoadOptions->set_ShouldAddPlainTextView(true);
htmlLoadOptions->set_PathToResources(dataDir);
MailMessage::Load(dataDir + L"description.html", emlLoadOptions);

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.

Loading EML and Saving as EML

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

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
// Initialize and Load an existing EML file by specifying the MessageFormat
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Attachments.eml");
mailMessage->Save(dataDir + L"LoadAndSaveFileAsEML_out.eml", SaveOptions::get_DefaultEml());

Loading EML and Saving as EML Preserving the Original Boundaries

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

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"Attachments.eml");
// Save as eml with preserved original boundares
System::SharedPtr<EmlSaveOptions> emlSaveOptions = System::MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat());
emlSaveOptions->set_PreserveOriginalBoundaries(true);
mailMessage->Save(dataDir + L"PreserveOriginalBoundaries_out.eml", emlSaveOptions);

Saving as EML Preserving TNEF Attachments

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

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// The path to the File directory.
System::String dataDir = RunExamples::GetDataDir_Email();
System::SharedPtr<MailMessage> mailMessage = MailMessage::Load(dataDir + L"PreserveOriginalBoundaries.eml");
// Save as eml with preserved attachment
System::SharedPtr<EmlSaveOptions> emlSaveOptions = System::MakeObject<EmlSaveOptions>(MailMessageSaveType::get_EmlFormat());
emlSaveOptions->set_FileCompatibilityMode(Aspose::Email::Mail::FileCompatibilityMode::PreserveTnefAttachments);
mailMessage->Save(dataDir + L"PreserveTNEFAttachment_out.eml", emlSaveOptions);

Loading EML, Saving 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.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Data directory for reading and writing files
System::String dataDir = RunExamples::GetDataDir_Email();
// Initialize and Load an existing EML file by specifying the MessageFormat
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml");
// Save the Email message to disk in ASCII format and Unicode format
eml->Save(dataDir + L"AnEmail_out.msg", SaveOptions::get_DefaultMsgUnicode());

Saving MailMessage as MHTML

Different options of MHTML can be used to obtain the desired results. The following code snippet shows you how to loads an EML message into MailMessage and converts it to MHTML.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Initialize and Load an existing EML file by specifying the MessageFormat
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml");
eml->Save(dataDir + L"AnEmail_out.mthml", SaveOptions::get_DefaultMhtml());

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 an email to MHT with customized TimeZone.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::SharedPtr<MailMessage> eml = MailMessage::Load(dataDir + L"Message.eml");
// Set the local time for message date.
eml->set_TimeZoneOffset(System::TimeZone::get_CurrentTimeZone()->GetUtcOffset(System::DateTime::get_Now()));
// or set custom time zone offset for message date (-0800)
// eml.TimeZoneOffset = new TimeSpan(-8,0,0);
// The dates will be rendered by local system time zone.
System::SharedPtr<MhtSaveOptions> so = System::MakeObject<MhtSaveOptions>();
so->set_MhtFormatOptions(Aspose::Email::Mail::MhtFormatOptions::WriteHeader);
eml->Save(dataDir + L"ExportEmailToMHTWithCustomTimezone_out.mhtml", so);

Exporting Email to EML

The following code snippet shows you how to export email to eml.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::String dataDir = RunExamples::GetDataDir_Email();
System::SharedPtr<MailMessage> msg = MailMessage::Load(dataDir + L"Message.eml");
msg->Save(dataDir + L"ExporttoEml_out.eml", SaveOptions::get_DefaultEml());