This project is read-only.

Adding Attachments and HTML Text to Outlook Msg File

Using this method, Microsoft Outlook must be installed on the machine where the code runs. The code snippet below creates an Outlook MSG file with attachments and HTML body.

// Create an object of type Outlook.Application
            Outlook.Application objOutlook = new Outlook.Application();

            //Create an object of type olMailItem
            Outlook.MailItem oIMailItem = objOutlook.CreateItem(Outlook.OlItemType.olMailItem);

            //Set properties of the message file e.g. subject, body and to address
            //Set subject
            oIMailItem.Subject = "This MSG file is created using Office Automation.";
            //Set to (recipient) address
            oIMailItem.To = "";
            //Set body of the email message
            oIMailItem.HTMLBody = "<html><p>This MSG file is created using VBA code.</p>";

            //Add attachments to the message

            //Save as Outlook MSG file

            //Open the MSG file


The code snippet below uses Aspose.Email for .NET library to create an MSG file, similar to the one created above, with multiple attachments and HTML body. Since Aspose.Email for .NET is purely written in .NET, COM interop is not required. Also, Microsoft Outlook 2003/2007 does not have to be installed on the machine. The method described below is suitable when Microsoft Outlook is not installed or when you want to generate MSG files on a server.

The code snippets below show how to perform the same task in C# using Aspose.Email for .NET.
// Create an instance of type MailMessage
            MailMessage msg = new MailMessage();

            // Set properties of message like subject, to and HTML body
            // Set subject
            msg.Subject = "This MSG file is created using Aspose.Email for .NET";
            // Set from (sender) address
            msg.Sender = new MailAddress("", "From Name");
            // Set to (recipient) address and name
            msg.To.Add(new MailAddress("", "To Name"));
            // Set HTML body of the email message
            msg.HtmlBody = @"<html><p>This MSG file is created using C# code.</p>" +
                "<p>Microsoft Outlook does not need to be installed on the machine running this code.</p>" +
                "<p>This method is suitable for creating MSG files on the server side.</html>";

            // Add attachments to the message file
            msg.Attachments.Add(new Attachment("image.bmp"));
            msg.Attachments.Add(new Attachment("pic.jpeg"));

            // Save as Outlook MSG file
            string strSaveFile ="TestAspose.msg";
            msg.Save(strSaveFile, MessageFormat.Msg);


Last edited Jan 1, 2014 at 7:34 AM by asposemarketplace, version 2