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.
- Create a new instance of the MailMessage class and set the From, To, Subject and Body properties.
- Call the MapiMessage class FromMailMessage method which accepts the object of the MailMessage type. The FromMailMessage method converts the MailMessage into a MapiMessage (MSG).
- 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.
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:
- When the Add Attachment button is clicked, an Open File Dialog is displayed to help users browse and select the attachment.
- When a file has been selected, the full path is added to a list.
- 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.
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.
Add the code for adding the attachments to the MailMessage instance. The final code for the Write Msg function is written as below.
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.
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.
RTF compression when setting the MAPI message body
NOTE: The compression process can slow down performance when creating messages. By understanding this fact and configuring the compression flag based on specific requirements and compromise between the file size and performance, developers can effectively manage the creation of MSG and PST files when dealing with email messages.
In this section you will learn how to use RTF compression when setting the MAPI message body. RTF compression is intended to reduce the size of a message as well as the resulting PST (Personal Storage Table) files that Microsoft Outlook uses to store e-mail messages and other data. By using RTF compression when configuring the message body, developers can reduce the amount of memory needed to store e-mail messages or optimize network bandwidth when transmitting messages.
For this purpose, there have been designed two overloaded methods:
MapiMessageItemBase.SetBodyContent(string content, BodyContentType contentType, bool compression): This method lets you set the message body content using the specified string content and specifying the body contentType (for example, plain text, HTML, etc.). The optional compression parameter is a value that specifies whether the content should be compressed using RTF compression. If the compression parameter is true, the content will be compressed, resulting in a smaller message size.
MapiMessageItemBase.SetBodyRtf(string content, bool compression): This method specifically sets the content of the message body in RTF format. The content parameter is a string representing the RTF content that will be set as the message body. As in the previous method, the compression parameter determines whether RTF compression should be applied to the content. If compression is true, the RTF content will be compressed to reduce the size.
The following code sample shows how to set html body and keep it compressed:
var msg = new MapiMessage("firstname.lastname@example.org", "email@example.com", "subject", "body");
// set the html body and keep it compressed
// this will reduce the message size
msg.SetBodyContent(htmlBody, BodyContentType.Html, true);
There is also a MapiConversionOptions.UseBodyCompression property. When this property is enabled, RTF body compression is applied during MailMessage to MapiMessage conversion, resulting in a smaller MSG file size. It is shown in the code sample below:
var message = MailMessage.Load(fileName);
var options = new MapiConversionOptions();
options.UseBodyCompression = true;
var msg = MapiMessage.FromMailMessage(message, options);