Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active March 22, 2022 14:29
Convert EML and MSG Emails to PDF in java
// Load email
FileInputStream fstream=new FileInputStream("email.eml");
MailMessage eml = MailMessage.load(fstream);
// Save the Message to output stream in MHTML format
ByteArrayOutputStream emlStream = new ByteArrayOutputStream();
eml.save(emlStream, SaveOptions.getDefaultMhtml());
// Load the stream in Word document
LoadOptions lo = new LoadOptions();
lo.setLoadFormat(LoadFormat.MHTML);
Document doc = new Document(new ByteArrayInputStream(emlStream.toByteArray()), lo);
// Save to disc
doc.save("converted.Pdf", SaveFormat.PDF);
// Or save to stream
ByteArrayOutputStream foStream = new ByteArrayOutputStream();
doc.save(foStream, SaveFormat.PDF);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment