Convert email to HTML in Java

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 Java, 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:

Java API to convert EML/MSG files to HTML

To accomplish the task of converting EML/MSG files to HTML, we will use the Aspose.Email for Java. This email processing API enables users to manage emails and work with file formats of popular email clients such as Thunderbird, Outlook, and others. To acquire the API, it is possible to either download or install it from our repository.

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

The code snippet below demonstrates the convertion process of 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 sets the ResourceRenderingMode property to SaveToFile.
  4. Defines an invoke method for the SaveResourceHandler of HtmlSaveOptions.
  5. Creates a target directory for the resources inside the invoke method if it does not already exist.
  6. Saves each resource to a file with a filename inside the invoke method 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 code snippet below demonstrates how to convert an MSG or EML file to HTML format in Java 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 an HTML with a header. The following code snippet demonstrates how to save an HTML with a header in Java:

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 formats to HTML in Java using Aspose.Email for Java library. You could see how quick and easy the process of conversion is just with a few lines of code. Overall, our API is an excellent solution for email management and processing in Java-based applications.

In case you have any questions, feel free to write to our free support forum. You can also learn more about Aspose.Email for Java, its features and facilities from our documentation.

See Also