Creating and Saving MSG Files

Aspose.Email supports creating Outlook message (MSG) files. This article explains how to:

  • Create MSG messages.
  • Create MSG messages with attachments.
  • Create a MSG message with an RTF body.
  • Save a message as a draft.
  • Work with body compression.

Creating and Saving Outlook Messages

The MailMessage class has the Save() method that can save Outlook MSG files to disk or stream. The code snippets below create an instance of the MailMessage class, set properties like from, to, subject and body. The Save() method takes the file name as an argument. In addition, the Outlook Messages can be created with a compressed RTF body using the MapiConversionOptions. To set up, create a new Windows application and add a reference to the Aspose.Email dll into the project.

  1. Create a new instance of the MailMessage class and set the From, To, Subject and Body properties.
  2. Call the MapiMessage class FromMailMessage method which accepts the object of the MailMessage type. The FromMailMessage method converts the MailMessage into a MapiMessage (MSG).
  3. Call the MapiMessage.Save() method to save the MSG file.

Write the following code in the click event of the button control of the Windows application.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = "sender@domain.com";
mailMsg.To = "receiver@domain.com";
mailMsg.Subject = "This is test message";
mailMsg.Body = "This is test body";
// Create an instance of the MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
// Save the message (MSG) file
string strMsgFile = @"CreatingAndSavingOutlookMessages_out.msg";
outlookMsg.Save(dataDir + strMsgFile);

Creating MSG Files With Attachments

In the example above, we created a simple MSG file. Aspose.Email also supports saving message files with attachments. All you need to do is to add the attachments to the MailMessage instance. Add attachments by calling the Add() method on the MailMessage.Attachments collection. Add a listbox to the form created above and add two buttons, one each for adding and removing attachments. The application that adds applications works like this:

  1. When the Add Attachment button is clicked, an Open File Dialog is displayed to help users browse and select the attachment.
  2. When a file has been selected, the full path is added to a list.
  3. When the MSG file is created, the attachment paths are grabbed from the list and added to the MailMessage.Attachments collection.

Write the following code in the Add Attachment button click event.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Open a file open dialog to select the attachment
OpenFileDialog fd = new OpenFileDialog();
// If user selected the file and clicked OK, add the file name and path to the list
if (fd.ShowDialog() == DialogResult.OK)
{
lstAttachments.Items.Add(fd.FileName);
}

When the Remove Attachment button is clicked, remove the selected items from the listbox. Write the following code in the Remove Attachment button click event.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Check if the user has selected any item in the attachments listbox
if (lstAttachments.SelectedItems.Count > 0)
{
// Remove the selected attachment from the listbox
lstAttachments.Items.RemoveAt(lstAttachments.SelectedIndex);
}

Add the code for adding the attachments to the MailMessage instance. The final code for the Write Msg function is written as below.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// File name for output MSG file
string strMsgFile;
// Open a save file dialog for saving the file and Add filter for msg files
SaveFileDialog fd = new SaveFileDialog();
fd.Filter = "Outlook Message files (*.msg)|*.msg|All files (*.*)|*.*";
// If user pressed OK, save the file name + path
if (fd.ShowDialog() == DialogResult.OK)
{
strMsgFile = fd.FileName;
}
else
{
// If user did not selected the file, return
return;
}
// Create an instance of MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = txtFrom.Text;
mailMsg.To = txtTo.Text;
mailMsg.Subject = txtSubject.Text;
mailMsg.Body = txtBody.Text;
// Add the attachments
foreach (string strFileName in lstAttachments.Items)
{
mailMsg.Attachments.Add(new Attachment(strFileName));
}
// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(strMsgFile);

Creating MSG Files With RTF Body

You can also create Outlook Message (MSG) files with rich text (RTF) bodies with Aspose.Email. The RTF body supports text formatting. Create one by setting the MailMessage.HtmlBody property. When you convert a MailMessage instance into a MapiMessage instance, the HTML body is converted into RTF. This way, the formatting of the email body is preserved.

The following example creates an MSG file with an RTF body. There is one heading, bold and underline formatting applied in the HTML body. This formatting is retained when the HTML is converted into RTF.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create an instance of the MailMessage class
MailMessage mailMsg = new MailMessage();
// Set from, to, subject and body properties
mailMsg.From = "from@domain.com";
mailMsg.To = "to@domain.com";
mailMsg.Subject = "subject";
mailMsg.HtmlBody = "<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Email.</p>";
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(dataDir + "CreatingMSGFilesWithRTFBody_out.msg");

Saving Message in Draft Status

Emails are saved as drafts when someone has started editing them but wants to return to them to complete them later. Aspose.Email supports saving email messages in draft status by setting a message flag. Below is the sample code to save an Outlook email message (MSG) as a draft.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Change properties of an existing MSG file
string strExistingMsg = @"message.msg";
// Load the existing file in MailMessage and Change the properties
MailMessage msg = MailMessage.Load(dataDir + strExistingMsg, new MsgLoadOptions());
msg.Subject += "NEW SUBJECT (updated by Aspose.Email)";
msg.HtmlBody += "NEW BODY (udpated by Aspose.Email)";
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it
MapiMessage mapiMsg = MapiMessage.FromMailMessage(msg);
mapiMsg.SetMessageFlags(MapiMessageFlags.MSGFLAG_UNSENT);
mapiMsg.Save(dataDir + "SavingMessageInDraftStatus_out.msg");

Implications of Body Compression

The RTF body compression method can be used to generate a smaller size MSG. However, this results in slower creation speed. To create messages with improved speed, set the flag to false. This flag, in turn, has an effect on the created PSTs: smaller MSG files result in smaller PST, and large MSG files result in slower PST creation.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
MailMessage message = MailMessage.Load(fileName);
MapiConversionOptions options = new MapiConversionOptions();
options.UseBodyCompression = true;
MapiMessage ae_mapi = MapiMessage.FromMailMessage(message, options);