Convert MSG and EML files to PDF in Java

Convert Emails to PDF in Java

Emails to PDF conversions are often required for referencing and requirements like sharing the email content. In this article, we will learn about how to convert email message files like MSG and EML into PDF using Java. Previously, in one of the earlier blog posts, we have already learned to convert MSG & EML files using C# in .NET application. This will help to automate the Email conversion within the desktop or web applications.

The following are the topics covered in this article:

Java Conversion Library

In this article, I will be using GroupDocs.Conversion for Java API for the conversions. By using it, you may convert email document formats like MSG and EML to PDF and other formats without losing the email format.

You may get the JAR file from the downloads section. For the maven based applications, following is the pom.xml configuration:

<repository>
	<id>GroupDocsJavaAPI</id>
	<name>GroupDocs Java API</name>
	<url>http://repository.groupdocs.com/repo/</url>
</repository>
<dependency>
        <groupId>com.groupdocs</groupId>
        <artifactId>groupdocs-conversion</artifactId>
        <version>20.6</version> 
</dependency>

Convert MSG to PDF using Java

The following are the steps to convert the Outlook MSG files to PDF with just a few lines of code. Embedded links in the steps will allow further exploring the classes and methods.

  • Create an instance of the Converter class and pass the MSG file to the constructor.
  • Instantiate the PdfConvertOptions class.
  • Call convert method to get the converted PDF file.

Here is the sample MSG file that is created using Microsoft Outlook. Further below is the PDF file, that is obtained by converting the MSG file using the above-mentioned java code.

MSG file to be converted to PDF
MSG file
Converted PDF file from MSG
PDF file converted from MSG format using the above Java code.

Convert EML to PDF using Java

We can programmatically convert our email messages stored in EML format, into PDF format with similar lines of java code very easily and efficiently. The following steps will guide to achieve the objective.

  • Initialize the Converter object providing the source EML file path.
  • Initialize the PDFConvertOptions. You may set further customization for the resultant PDF file.
  • Just call the convert method of the Converter class and pass it the resultant PDF file path and the already set PDFConvertOptions as parameters.

Shown below are the source EML file and the converted PDF file screenshots, that has been converted using the above java code.

EML file to be converted to PDF
EML file
Converted PDF file from EML
PDF file converted from EML format using Java.

Conclusion

In this article, we learned how to convert the MSG and EML files to PDF using Java Conversion API. Furthermore, we can programmatically apply customization on PDF files to get the outcome in our desired style. You may get some more about GroupDocs.Conversion for Java from the documentation.

See Also