Aspose.Network also supports creating an outlook message (msg) files. The MailMessage class has the Save() method that can save the outlook msg file to disk or stream. In the code snippet below, we will create an instance of the MailMessage class, set some properties like from, to, subject, body.. The save() method takes the file name as an argument.
Create a new windows application and add a reference to the Aspose.Network dll in the project. Design the UI of the form as shown in the screenshot below.
We will create a new instance of MailMessage class and set properties like From, To, Subject and Body. Later on we will call FromMailMessage() method of MapiMessage class which accepts object of type MailMessage. FromMailMessage() method will convert the MailMessage into MapiMessage (msg) and then we will call MapiMessage.Save() method to finally save the msg file.
Write the following code on the click event of the button control.
[C#]
// 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.TextBody = txtBody.Text;
// Create an instance of MapiMessage class and pass MailMessage as argument
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
// Path and file name where message file will be saved
string strMsgFile = @"E:\Data\Aspose\temp\sample.msg";
// save the message (msg) file
outlookMsg.Save(strMsgFile);
[VB.NET]
' Create an instance of MailMessage class
Dim mailMsg As MailMessage = New MailMessage()
' Set from, to, subject and body properties
mailMsg.From = txtFrom.Text
mailMsg.To = txtTo.Text
mailMsg.Subject = txtSubject.Text
mailMsg.TextBody = txtBody.Text
' Create an instance of MapiMessage class and pass MailMessage as argument
Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(mailMsg)
' Path and file name where message file will be saved
Dim strMsgFile As String = "E:\Data\Aspose\temp\sample.msg"
' save the message (msg) file
outlookMsg.Save(strMsgFile)
If you open the saved file in Microsoft Outlook, it will look like the screenshot below.
Creating Msg Files With Attachments
In the above example, we created a simple msg file. Aspose.Network also supports saving message files with attachments. All you need to do is to add the attachments to the MailMessage instance. We can add attachments by calling Add() method on the MailMessage.Attachments collection.
Add a listbox to the form that you created in the above example and add two buttons for adding and removing the attachments. The form GUI should look like as below.
On click event of Add Attachment button, we will open an Open File Dialog to browse and select the attachment. If the user has selected the file, we will add the full path to the listbox. Later on, while creating the msg file, we will pick the path of the attachments from the listbox and add to the MailMessage.Attachments collection.
Write the following code on the click event of the Add Attachment button.
[C#]
// open a file open dialog to select the attachment
OpenFileDialog fd = new OpenFileDialog();
// if user selected the file and pressed OK, add the file name and path to the list
if (fd.ShowDialog() == DialogResult.OK)
{
lstAttachments.Items.Add(fd.FileName);
}
[VB.NET]
' open a file open dialog to select the attachment
Dim fd As OpenFileDialog = New OpenFileDialog()
' if user selected the file and pressed OK, add the file name and path to the list
If fd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
lstAttachments.Items.Add(fd.FileName)
End If
On the click event of Remove Attachment button, we will simply remove the selected items from the listbox. Write the following code on the click event of Remove Attachment button.
[C#]
// 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);
}
[VB.NET]
' check if the user has selected any item in the attachments listbox
If lstAttachments.SelectedItems.Count > 0 Then
' remove the selected attachment from the listbox
lstAttachments.Items.RemoveAt(lstAttachments.SelectedIndex)
End If
Now, we will add the code for adding the attachments to the MailMessage instance. The final code for the Write Msg function is written as below.
[C#]
// Path and file name where message file will be saved
string strMsgFile;
// open a save file dialog for saving the file
SaveFileDialog fd = new SaveFileDialog();
// add a filter for msg files
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.TextBody = 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);
// save the message (msg) file
outlookMsg.Save(strMsgFile);
[VB.NET]
' Path and file name where message file will be saved
Dim strMsgFile As String
' open a save file dialog for saving the file
Dim fd As SaveFileDialog = New SaveFileDialog()
' add a filter for msg files
fd.Filter = "Outlook Message files (*.msg)|*.msg|All files (*.*)|*.*"
' if user pressed OK, save the file name + path
If fd.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
strMsgFile = fd.FileName
Else
' if user did not selected the file, return
Return
End If
' Create an instance of MailMessage class
Dim mailMsg As MailMessage = New MailMessage()
' Set from, to, subject and body properties
mailMsg.From = txtFrom.Text
mailMsg.To = txtTo.Text
mailMsg.Subject = txtSubject.Text
mailMsg.TextBody = txtBody.Text
' Add the attachments
For Each strFileName As String In lstAttachments.Items
mailMsg.Attachments.Add(New Attachment(strFileName))
Next strFileName
' Create an instance of MapiMessage class and pass MailMessage as argument
Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(mailMsg)
' save the message (msg) file
outlookMsg.Save(strMsgFile)
Following is the screenshot for the msg file that is created with this example.
Complete code (Visual Studio project) can be found in the Downloads à Aspose.Network à Demos à “Creating Outlook Message (msg) Files” section of www.aspose.com.
Creating Msg Files With RTF Body
You can also create Outlook Message (msg) files with Rtf body with Aspose.Network. Rtf body supports text formatting. You can accomplish it by setting MailMessage.HtmlBody property. When you convert MailMessage instance into MapiMessage instance, the HTML body is converted into Rtf body. Thus, you can preserve formatting of your email body.
In the following example, we will create an msg file with Rtf body. There is one heading, bold and underline formatting applied in the HtmlBody. This would be converted correctly into Rtf Body.
[C#]
// Create an instance of 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.Network.</p>”;
MapiMessage outlookMsg = MapiMessage.FromMailMessage(mailMsg);
outlookMsg.Save(“test.msg”);
[VB.NET]
' Create an instance of MailMessage class
Dim mailMsg As MailMessage = New MailMessage()
' Set from, to, subject and body properties
mailMsg.From = “fromdomain.com”
mailMsg.To = “todomain.com”
mailMsg.Subject = “subject”
mailMsg.HtmlBody = “<h3>rtf example</h3><p>creating an <b><u>outlook message (msg)</u></b> file using Aspose.Network.</p>”
Dim outlookMsg As MapiMessage = MapiMessage.FromMailMessage(mailMsg)
outlookMsg.Save(“test.msg”)
The output msg file is shown in the screenshot below.