Home > Code > C# > Convert Microsoft Outlook MSG Email Message to PDF File

Convert Microsoft Outlook MSG Email Message to PDF File

by sher azam   on Jun 24, 2013   Category: C#   |  Views: 2329    |  Points: 25   |  Gold 


This technical tip explains how to convert an email message to PDF using Aspose.Email. Aspose.Email for .NET deals with network protocols and Microsoft Outlook features, and cannot handle direct conversion to PDF. To overcome this, the samples in this article use Aspose.Email to convert the email message to MHTML stream and then use Aspose.Words for .NET to load the MHTML stream and then save it as PDF. The following code shows converting email message to PDF using Aspose.Email in combination with Aspose.Words for .NET. This involves the following steps:

1. Load the email message using MailMessage
2. Save the email message to MemoryStream as MHTML
3. Load the stream using Aspose.Words
4. Save the message as PDF

Sample Code for Converting an Email Message to PDF

[C#]

MailMessage mailMsg = MailMessage.Load("About Aspose-1.msg");

MemoryStream ms = new MemoryStream();

//Save as MHTML
mailMsg.Save(ms, MailMessageSaveType.MHtmlFromat);

//create an instance of LoadOptions and set the LoadFormat to Mhtml
var loadOptions = new Aspose.Words.LoadOptions();
loadOptions.LoadFormat = LoadFormat.Mhtml;

//create an instance of Document and load the MTHML from MemoryStream
var document = new Aspose.Words.Document(ms, loadOptions);

//create an instance of HtmlSaveOptions and set the SaveFormat to Html
var saveOptions = new Aspose.Words.Saving.PdfSaveOptions();

//save the document to Html file
document.Save("About Aspose.pdf", saveOptions);

[VB.NET]

Dim mailMsg As MailMessage = MailMessage.Load("About Aspose-1.msg")

Dim ms As New MemoryStream()

'Save as MHTML
mailMsg.Save(ms, MailMessageSaveType.MHtmlFromat)

'create an instance of LoadOptions and set the LoadFormat to Mhtml
Dim loadOptions = New Aspose.Words.LoadOptions()
loadOptions.LoadFormat = LoadFormat.Mhtml

'create an instance of Document and load the MTHML from MemoryStream
Dim document = New Aspose.Words.Document(ms, loadOptions)

'create an instance of HtmlSaveOptions and set the SaveFormat to Html
Dim saveOptions = New Aspose.Words.Saving.PdfSaveOptions()

'save the document to Html file
document.Save("About Aspose.pdf", saveOptions)

More about Aspose.Email for .NET

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx

- More Technical Tips by Aspose.Email for .NET: http://docs.aspose.com:8082/docs/display/emailnet/Technical+Articles



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts