Convert MHTML to Other Formats

An MHTML file (MHT) is a web page packed together with its images into a single MIME document - the format browsers use for “save page as single file”, and the one email programs use to export a message for viewing. Because the content is MIME, Aspose.Email for C++ reads it into a MailMessage with MhtmlLoadOptions, and from there it can be saved in any message format.

The samples in this article use these headers and namespaces:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <AttachmentCollection.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <MhtmlLoadOptions.h>
#include <Mapi/MapiMessage.h>
#include <Storage/Mbox/MboxrdStorageWriter.h>
#include <Storage/Mbox/MboxSaveOptions.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Mbox;
using namespace Aspose::Email::Storage::Pst;

Convert MHTML to EML, MSG, HTML and OFT

Once the file is loaded, each target format is one Save call with the matching default options. HTML unpacks the page from its container, EML and MSG turn it into a message, and OFT into an Outlook template.

SharedPtr<MailMessage> message = MailMessage::Load(u"sample.mhtml", MakeObject<MhtmlLoadOptions>());

message->Save(u"from-mhtml.eml", SaveOptions::get_DefaultEml());
message->Save(u"from-mhtml.msg", SaveOptions::get_DefaultMsgUnicode());
message->Save(u"from-mhtml.html", SaveOptions::get_DefaultHtml());
message->Save(u"from-mhtml.oft", SaveOptions::get_DefaultOft());

Console::WriteLine(u"Attachments carried over: " + Convert::ToString(message->get_Attachments()->get_Count()));

An MHTML file has no sender or recipients of its own. Set them on the loaded message before saving when the result should look like a real email:

SharedPtr<MailMessage> message = MailMessage::Load(u"sample.mhtml", MakeObject<MhtmlLoadOptions>());

message->set_From(MakeObject<MailAddress>(u"archive@example.com", String(u"Web archive")));
message->get_To()->Add(MakeObject<MailAddress>(u"team@example.com"));
message->set_Subject(u"Saved page");

message->Save(u"from-mhtml-addressed.msg", SaveOptions::get_DefaultMsgUnicode());

Add MHTML Files to an MBOX File

MboxrdStorageWriter appends each loaded file as one message of the MBOX storage.

SharedPtr<MboxrdStorageWriter> writer = MakeObject<MboxrdStorageWriter>(u"from-mhtml.mbox", MakeObject<MboxSaveOptions>());
writer->WriteMessage(MailMessage::Load(u"sample.mhtml", MakeObject<MhtmlLoadOptions>()));
writer->Dispose();

Add MHTML Files to a PST File

MapiMessage::FromMailMessage turns the loaded message into the MAPI form a PST stores.

SharedPtr<MailMessage> message = MailMessage::Load(u"sample.mhtml", MakeObject<MhtmlLoadOptions>());

SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"from-mhtml.pst", FileFormatVersion::Unicode);
SharedPtr<FolderInfo> folder = pst->CreatePredefinedFolder(u"Inbox", StandardIpmFolder::Inbox);
folder->AddMessage(MapiMessage::FromMailMessage(message));

Console::WriteLine(u"Messages in the PST: " + Convert::ToString(folder->get_ContentCount()));
pst->Dispose();