Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-kb/022ada083c33b97301787a210cc2d694 to your computer and use it in GitHub Desktop.
Save aspose-com-kb/022ada083c33b97301787a210cc2d694 to your computer and use it in GitHub Desktop.
How to Convert Outlook Email Message to PDF using Java. For more information please follow link: https://kb.aspose.com/email/java/how-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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment