Create and Save Outlook MSG files
Aspose.Email for C++ allows developers to programmatically create, modify, and save Outlook MSG files with full control over message properties and formatting. You can generate MSG messages from scratch, add attachments, use rich text (RTF) bodies, save drafts, and optimize message size using body compression options.
The samples in this article use these headers and namespaces:
#include <MailMessage.h>
#include <MsgLoadOptions.h>
#include <Attachment.h>
#include <AttachmentCollection.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiMessageFlags.h>
#include <Mapi/MapiConversionOptions.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
Create and Save Outlook Messages
A message is usually composed as a MailMessage: set the sender, the recipients, the subject and the body, then turn it into an Outlook item with MapiMessage::FromMailMessage. MapiMessage::Save writes the item as an MSG file that Outlook opens directly.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"CreatedMessage.msg");
Console::WriteLine(u"Saved: " + MapiMessage::Load(u"CreatedMessage.msg")->get_Subject());
Add Recipients and Attachments to a MapiMessage
A MapiMessage can also be filled in directly, without converting a MailMessage. The Add methods of the MapiRecipientCollection class return the created MapiRecipient, and the Add methods of the MapiAttachmentCollection class return the created MapiAttachment. This lets you configure a new recipient or attachment immediately, without a separate lookup in the collection.
The recipient type is one of the MapiRecipientType values. The following code sample adds a recipient and an attachment and then changes their display names:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiRecipientType.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email::Mapi;
auto message = MakeObject<MapiMessage>();
message->set_Subject(u"Invoice");
// Add a recipient and configure it
SharedPtr<MapiRecipient> recipient = message->get_Recipients()->Add(u"alice.johnson@example.com", u"SMTP", u"Alice Johnson", MapiRecipientType::MAPI_TO);
recipient->set_DisplayName(u"Alice Johnson (Accounting)");
// Add an attachment from file bytes and configure it
ArrayPtr<uint8_t> fileBytes = IO::File::ReadAllBytes(u"invoice.pdf");
SharedPtr<MapiAttachment> attachment = message->get_Attachments()->Add(u"invoice.pdf", fileBytes);
attachment->set_DisplayName(u"Invoice #2026-001.pdf");
message->Save(u"invoice.msg");
Create MSG Files with RTF Body
Outlook keeps the formatted body of a message as Rich Text Format (RTF). When a MailMessage with an HTML body is converted, Aspose.Email builds the RTF body from the HTML, so headings, bold and underlined text survive in the MSG file. The RTF text is available through get_BodyRtf.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"from@domain.com");
mailMsg->set_To(u"to@domain.com");
mailMsg->set_Subject(u"RTF example");
mailMsg->set_HtmlBody(u"<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>");
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithRtfBody.msg");
String rtf = MapiMessage::Load(u"MessageWithRtfBody.msg")->get_BodyRtf();
Console::WriteLine(u"RTF body starts with: " + rtf.Substring(0, 6));
Save a Message in Draft Status
Outlook shows a message as a draft - editable and with a Send button - when the MSGFLAG_UNSENT flag is set on it. The sample loads an existing MSG file as a MailMessage (with MsgLoadOptions), changes it, and saves it back as a draft by passing the flag to SetMessageFlags.
SharedPtr<MailMessage> msg = MailMessage::Load(u"Message.msg", MakeObject<MsgLoadOptions>());
msg->set_Subject(msg->get_Subject() + u" (updated by Aspose.Email)");
msg->set_HtmlBody(u"<p>NEW BODY (updated by Aspose.Email)</p>");
SharedPtr<MapiMessage> mapiMsg = MapiMessage::FromMailMessage(msg);
mapiMsg->SetMessageFlags(MapiMessageFlags::MSGFLAG_UNSENT);
mapiMsg->Save(u"DraftMessage.msg");
bool isDraft = (MapiMessage::Load(u"DraftMessage.msg")->get_Flags() & MapiMessageFlags::MSGFLAG_UNSENT) == MapiMessageFlags::MSGFLAG_UNSENT;
Console::WriteLine(u"Saved as a draft: " + Convert::ToString(isDraft));
Optimizing with Body Compression
The body of an MSG file can be stored compressed. MapiConversionOptions::set_UseBodyCompression switches the compression on for the conversion; the resulting files - and PST files built from such messages - are smaller, at the cost of a little extra processing time.
SharedPtr<MailMessage> message = MailMessage::Load(u"Message.eml");
SharedPtr<MapiConversionOptions> options = MakeObject<MapiConversionOptions>();
options->set_UseBodyCompression(true);
MapiMessage::FromMailMessage(message, options)->Save(u"CompressedBody.msg");
MapiMessage::FromMailMessage(message)->Save(u"UncompressedBody.msg");
Console::WriteLine(u"Compressed body: " + Convert::ToString(IO::File::ReadAllBytes(u"CompressedBody.msg")->get_Length()) + u" bytes");
Console::WriteLine(u"Uncompressed body: " + Convert::ToString(IO::File::ReadAllBytes(u"UncompressedBody.msg")->get_Length()) + u" bytes");
- UseBodyCompression = true → smaller file size, slower performance.
- UseBodyCompression = false → faster conversion, larger output.
Create an MSG File with Attachments
Attachments are added to the MailMessage before it is converted: every Attachment in the Attachments collection becomes an attachment of the resulting Outlook message. Building the item directly as a MapiMessage is described in Manage Attachments in Outlook MSG Files.
SharedPtr<MailMessage> mailMsg = MakeObject<MailMessage>();
mailMsg->set_From(u"sender@domain.com");
mailMsg->set_To(u"receiver@domain.com");
mailMsg->set_Subject(u"This is a test message");
mailMsg->set_Body(u"This is a test body");
// every file added here becomes an attachment of the MSG
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"1.jpg"));
mailMsg->get_Attachments()->Add(MakeObject<Attachment>(u"description.html"));
SharedPtr<MapiMessage> outlookMsg = MapiMessage::FromMailMessage(mailMsg);
outlookMsg->Save(u"MessageWithAttachments.msg");
Console::WriteLine(u"Attachments in the saved message: " +
Convert::ToString(MapiMessage::Load(u"MessageWithAttachments.msg")->get_Attachments()->get_Count()));