How to Convert Outlook Email to PDF using Java

In this simple tutorial, we will focus on how to convert Outlook Email to PDF using Java along with detailed steps to configure the environment. You will observe that in order to convert MSG to PDF Java based API can be used. Moreover, this application can be used in any of the commonly available operating systems like Windows, macOS, or Linux.

Steps to Convert Outlook Email to PDF using Java

  1. Install Aspose.Email and Aspose.Words JAR file from Maven repository
  2. Instantiate the MailMessage class object to load the EML or MSG email file from the disk
  3. Convert the loaded Email (MSG/EML) file as a ByteArrayOutputStream to an intermediate MHTML format
  4. Instantiate the Document class object to load the intermediate MHTML file from stream by setting the LoadFormat to MHTML
  5. Save the loaded MHTML to PDF using the save method

By following the aforementioned steps in Java MSG to PDF converter application is developed, whereby the process starts by loading the EML or MSG file from the disk using the MailMessage class object and then converted to an intermediate stream in an MHTML format using the save method. We will then load the intermediate MHTML using the Document class object and for converting EML to PDF Java based save method will be used.

Code to Convert Outlook Email to PDF using Java

import com.aspose.email.MailMessage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class EmailToPDFConverter {
public static void main(String[] args) throws Exception {
// Apply the Aspose.Email product license to read the Email (MSG/EML) file
com.aspose.email.License EmailLic = new com.aspose.email.License();
EmailLic.setLicense("Aspose.Total.lic");
// Apply the Aspose.Words product license to convert MHTML to PDF
com.aspose.words.License WordsLic = new com.aspose.words.License();
WordsLic.setLicense("Aspose.Total.lic");
// Create an OutputStream object to hold intermediate MHTML
ByteArrayOutputStream ms = new ByteArrayOutputStream();
// Load the source MSG or EML file from the disk
MailMessage message = MailMessage.load("Message.msg");
// Save the loaded MSG/EML as MHTML
message.save(ms, com.aspose.email.SaveOptions.getDefaultMhtml());
// Initialize the LoadOptions to set the LoadFormat to Mhtml
com.aspose.words.LoadOptions loadOptions = new com.aspose.words.LoadOptions();
loadOptions.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
// Instantiate Document class object to load the MTHML from MemoryStream
com.aspose.words.Document document = new com.aspose.words.Document(
new ByteArrayInputStream(ms.toByteArray()), loadOptions);
// Initialiize the PdfSaveOptions class object
com.aspose.words.PdfSaveOptions saveOptions = new com.aspose.words.PdfSaveOptions();
// Save the MHTML to PDF using Java
document.save("SaveEmailAsPDF.pdf", saveOptions);
}
}

In order to convert EML to PDF Java based API is used with the help of simple API calls. The above example is useful for loading and converting both MSG and EML files format. The output PDF can be customized by setting properties like Compliance, ColorMode, ImageCompression and JpegQuality.

To convert MSG to PDF Java based API is explored in this article. If you are looking for how to convert EML to MSG using Java, refer to the article on how to convert EML to MSG using Java.

 English