EML and MSG are popular email formats used by MS Outlook to store emails. There could be the cases when you need to share the content of EML or MSG files from within your web or desktop applications. One of the most suitable solutions is the conversion of email files to PDF format. In this article, you will learn how to convert an EML or MSG email to PDF programmatically in Java.
Java EML and MSG to PDF Converter APIs
To convert email files to PDF, we will use the combination of Aspose.Email for Java and Aspose.Words for Java. The former is an email API that provides a bunch of features to create email client applications. Whereas, the latter one is a word processing API for creating and manipulating Word documents. You can use the following Maven configurations to install the API in your Java applications.
Repository: <repository> <id>AsposeJavaAPI</id> <name>Aspose Java API</name> <url>http://repository.aspose.com/repo/</url> </repository> Dependencies: <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-email</artifactId> <version>22.2</version> <classifier>jdk16</classifier> </dependency> <dependency> <groupId>com.aspose</groupId> <artifactId>aspose-words</artifactId> <version>22.3</version> <type>pom</type> </dependency>
Also, you can download the JARs of the APIs from the following links.
Convert an EML or MSG File to PDF in Java
The following are the steps to convert an EML or MSG email to PDF format in Java.
- Load the email file using MailMessage class.
- Save the email message into a ByteArrayOutputStream object as default MHTML format.
- Create an instance of LoadOptions class and set the load format as MHTML.
- Instantiate Document class and pass ByteArrayOutputStream and LoadOptions objects as parameters to its constructor.
- Convert the email to PDF using Document.save(String, SaveFormat.PDF) method.
The following is the complete code to convert an email message to PDF in Java.
Get a Free License
You can get a free temporary license in order to try the APIs without evaluation limitations.
In this article, you have learned how to convert EML or MSG email messages to PDF in Java. We have demonstrated how to use the combination of Aspose.Email and Aspose.Words to implement email to PDF conversion from within Java applications. You can also explore the below-mentioned documentations of both of the APIs. Also, you can post your queries to our forum.