How to Convert an Email (EML) File to a JPG/JPEG Image Array using Java

Cloudmersive
2 min readOct 18, 2022

--

Email (EML) files are a standard message file format for common email applications like Microsoft Outlook & more. As with any file bearing important written contents, it’s great to have backups of that content on hand — and common/lightweight file types like JPG (or PNG) represent a very suitable backup format.

It may be tempting to save images of emails with manual screenshots, there’s a much better way to do it: using the Cloudmersive EML to JPG Conversion API. As the title suggests, this API will convert input EML files to a JPG Image Array (one JPG file for each page of an email). This API also allows you to determine the quality of your output JPG file (1–100 scale; 1 = highest compression, 100 = lowest compression, 75 = default value) so you can reduce or increase each file’s quality at will.

Below, I’ll demonstrate how you can take advantage of this API for free & structure your API call using complementary, ready-to-run Java code examples from our API console page. All you need to do is get a Cloudmersive API key (you can get one for free by registering a free account on our website), copy & paste, and you’re done.

To get started, we’ll first need to install the Document Conversion API client. We can do so by adding a reference to the repository in pom.xml:

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

And then adding 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, we can add the imports and call the function. Within the code examples below, make sure to add your API key, file path & file compression integer (if you elect to customize that option) in their respective fields:

// 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;
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.
Integer quality = 56; // Integer | Optional; Set the JPEG quality level; lowest quality is 1 (highest compression), highest quality (lowest compression) is 100; recommended value is 75. Default value is 75.
try {
EmlToJpgResult result = apiInstance.convertDocumentEmlToJpg(inputFile, quality);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling ConvertDocumentApi#convertDocumentEmlToJpg");
e.printStackTrace();
}

After that, you’re all done — no more code required. Nice and easy.

--

--

Cloudmersive

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