How to Convert an Email (EML) File to an HTML String using Java

Cloudmersive
2 min readOct 18, 2022

--

Within most email applications, you’ll have the option to convert individual Email (EML) files to other formats like HTML. While convenient for small-scale uses, this feature is a bit impractical for larger-scale conversion needs — and that’s where our EML conversion APIs can make a big difference. In this article, I’ll demonstrate how you can use our EML to HTML API to convert important email files quickly & securely using only the file path. This API also provides two optional parameters allowing you to specify if the HTML string should include ONLY the body of the email (or additional information, like the email subject; default if false) and if the response object should include any attachments from the input file (default is true). To use this API for free, all you need to do is structure your API call with the code examples provided below and register a free account on our website to get a free-tier API key (which provides a limit of 800 API calls per month).

Our first step is to install the API client. Let’s first add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

And then let’s add a reference to the dependency in pom.xml:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

With installation complete, let’s now add the imports to the top of our file:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.ConvertDocumentApi;

And then let’s call the function, adding our file path & API key in their respective fields (indicated by the code comments):

ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
ConvertDocumentApi apiInstance = new ConvertDocumentApi();
File inputFile = new File("/path/to/inputfile"); // File | Input file to perform the operation on.
Boolean bodyOnly = true; // Boolean | Optional; If true, the HTML string will only include the body of the email. Other information such as subject will still be given as properties in the response object. Default is false.
Boolean includeAttachments = true; // Boolean | Optional; If false, the response object will not include any attachment files from the input file. Default is true.
try {
EmlToHtmlResult result = apiInstance.convertDocumentEmlToHtml(inputFile, bodyOnly, includeAttachments);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentEmlToHtml");
e.printStackTrace();
}

Don’t forget you can set boolean values to specify how much your information your output HTML file will include. After that, you’re all done!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet