Convert email to HTML in C# .NET

HTML is a widely used and easily accessible format that can be viewed by a wide range of platforms, web browsers, including desktop computers and mobile devices. In contrast, EML and MSG files may require specific software or email clients to be viewed, which can make them more difficult to access and view over time. So, converting MSG or EML files to HTML makes them easier to view, particularly for recipients who may not have compatible email clients or software.

Additionally, HTML files are easier to archive and remain in a searchable, indexed format.

This article presents an algorithm for swiftly and easily converting MSG or EML files to HTML format using C#, regardless of the underlying purpose. Our reliable method guarantees that the content of the email, including embedded images, is fully preserved without any loss. This ensures that the integrity of the email is maintained throughout the conversion process.

The article covers the following topics:

C# API to convert EML/MSG files to HTML

For the conversion of EML/MSG files to HTML, we’re going to use Aspose.Email for .NET. It is an email processing API that lets you manipulate emails and work with file formats of popular email clients such as Thunderbird, Outlook, etc. You can download the API’s DLL or install it from NuGet using the following command:

PM> Install-Package Aspose.Email

Convert EML/MSG files to HTML saving Images Separately

A typical MSG file has the following view:

MSG file view

With our API you can set any directory on your device and save the target HTML file with images in a separate folder:

HTML file view

Use the following code example to convert an email message in MSG or EML format to an HTML file with images, which are saved to a target directory. The code performs the following steps:

  1. First, specifies the source and target paths for the source MSG/EML file and the HTML file, respectively, as well as the target directory for the resources.
  2. Loads the MSG or EML file using the MailMessage.Load method.
  3. Creates HtmlSaveOptions object and set the ResourceRenderingMode property to SaveToFile.
  4. Defines a delegate for the SaveResourceHandler event of HtmlSaveOptions.
  5. Creates a target directory for the resources inside the delegate if it does not already exist.
  6. Saves each resource to a file with a filename inside the delegate based on the content type of the resource.
  7. Calls the Save method of the MailMessage instance, passing in the target HTML file name and the HtmlSaveOptions instance to save the email in HTML format.

Convert EML/MSG files to HTML saving Images inside HTML

Sometimes you may prefer to convert EML/MSG files to HTML retaining embedded images inside HTML.

Image inside HTML in a text editor

The following code snippet demonstrates how to convert an MSG or EML file to HTML format in C# and save the images inside HTML. The code performs the following steps:

  1. Defines the source and target paths for the email file and HTML output.
  2. Loads the MSG or EML file using the MailMessage.Load method.
  3. Creates a new instance of the HtmlSaveOptions class, which allows you to configure how the email is saved as HTML.
  4. Configures the HtmlSaveOptions instance to embed the resources, including images, directly into the HTML.
  5. Calls the Save method of the MailMessage instance, passing in the target HTML file name and the HtmlSaveOptions instance to save the email in HTML format.

Saving HTML with a Header

As an optional variant you can save a HTML with a header. The following code snippet demonstrates how to save an HTML with a header in C#:

As a result, a text header will be added to the target HTML:

Text header

Conclusion

To sum up, from this article you have just learnt about the convenience of HTML format for archiving and managing your files and how to excecute conversion from EML or MSG in C# using Aspose.Email for .NET library. You could see how quick and easy the process of conversion is just with a few lines of code. In case you have any questions, feel free to write to our free support forum. You can also learn more about Aspose.Email for .NET, its features and facilities from our documentation.

See Also