Convert HTML to Other Formats

An HTML page becomes an email by loading it into a MailMessage: the markup turns into the HTML body, and the images the page refers to are embedded as linked resources. HtmlLoadOptions controls the loading - PathToResources tells the library where to look for images given by relative paths, and ShouldAddPlainTextView adds a plain-text alternative for mail programs that do not show HTML.

The samples in this article use these headers and namespaces:

#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MhtSaveOptions.h>
#include <MsgSaveOptions.h>
#include <HtmlLoadOptions.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 HTML to EML, MSG, MHTML and OFT

A page has no sender, recipients or subject, so they are set on the loaded message before it is saved. Each target format is then one Save call.

SharedPtr<HtmlLoadOptions> options = MakeObject<HtmlLoadOptions>();
options->set_PathToResources(u".");          // where images with relative paths are found
options->set_ShouldAddPlainTextView(true);   // a text/plain alternative next to the HTML body

SharedPtr<MailMessage> message = MailMessage::Load(u"body_content.html", options);
message->set_From(MakeObject<MailAddress>(u"newsletter@example.com", String(u"Newsletter")));
message->get_To()->Add(MakeObject<MailAddress>(u"reader@example.com"));
message->set_Subject(u"This week's newsletter");

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

Console::WriteLine(u"HTML body: " + Convert::ToString(message->get_IsBodyHtml()) +
    u", plain-text alternative: " + Convert::ToString(!String::IsNullOrEmpty(message->get_Body())));

Add HTML Pages to an MBOX File

SharedPtr<MailMessage> message = MailMessage::Load(u"body_content.html", MakeObject<HtmlLoadOptions>());
message->set_Subject(u"Page saved to MBOX");

SharedPtr<MboxrdStorageWriter> writer = MakeObject<MboxrdStorageWriter>(u"from-html.mbox", MakeObject<MboxSaveOptions>());
writer->WriteMessage(message);
writer->Dispose();

Add HTML Pages to a PST File

MapiMessage::FromMailMessage converts the loaded page into the MAPI form a PST stores. A folder such as Drafts suits content that has not been sent anywhere.

SharedPtr<MailMessage> message = MailMessage::Load(u"body_content.html", MakeObject<HtmlLoadOptions>());
message->set_Subject(u"Page saved to PST");

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

Console::WriteLine(u"Messages in Drafts: " + Convert::ToString(drafts->get_ContentCount()));
pst->Dispose();