Convert OFT to Other Formats
An Outlook template (OFT) is an MSG file marked as a template: Outlook opens it as a new, unsent message. Converting a template usually means filling it in - setting recipients, replacing placeholders - and saving the result as a regular message. Aspose.Email for C++ loads a template with MapiMessage::Load, which keeps all Outlook properties, or with MailMessage::Load, which gives the MIME view used for EML and web formats. IsTemplate tells a template from an ordinary MSG file.
The samples in this article use these headers and namespaces:
#include <MailMessage.h>
#include <MailAddressCollection.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <MhtSaveOptions.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientType.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
Convert OFT to MSG
Saving a loaded template with MapiMessage::Save writes an ordinary MSG file - the template mark is not carried over - so the result opens in Outlook as a normal message. Fill in what the template leaves open before saving.
SharedPtr<MapiMessage> message = MapiMessage::Load(u"greeting.oft");
Console::WriteLine(u"Loaded a template: " + Convert::ToString(message->get_IsTemplate()));
// complete the message the template describes
message->get_Recipients()->Add(u"customer@example.com", u"Customer", MapiRecipientType::MAPI_TO);
message->Save(u"from-oft.msg");
Console::WriteLine(u"Saved as a template: " + Convert::ToString(MapiMessage::Load(u"from-oft.msg")->get_IsTemplate()));
Convert OFT to EML, HTML and MHTML
For MIME-based formats the template is loaded as a MailMessage. Its body is ordinary text or HTML, so placeholders can be replaced with Replace before the result is saved.
SharedPtr<MailMessage> message = MailMessage::Load(u"greeting.oft");
// personalize the body of the template
message->set_HtmlBody(message->get_HtmlBody().Replace(u"[Name]", u"Ada"));
message->get_To()->Add(u"ada@example.com");
message->Save(u"from-oft.eml", SaveOptions::get_DefaultEml());
message->Save(u"from-oft.html", SaveOptions::get_DefaultHtml());
message->Save(u"from-oft.mhtml", SaveOptions::get_DefaultMhtml());
How templates are created and filled in field by field is covered in Outlook Templates, Signed Messages & Categories.