Save Email Message As PDF
Contents
[
Hide
]
Aspose.Email - Save Email Message As PDF
The following code shows converting email message to PDF using Aspose.Email in combination with Aspose.Words for Java. This involves the following steps:
- Load the email message using MailMessage
- Save the email message to MemoryStream as MHTML
- Load the stream using Aspose.Words
- Save the message as PDF
Java
FileInputStream fstream = new FileInputStream(dataDir + "message.msg");
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 into a Word document; the Aspose.Words types are qualified
// because com.aspose.email has a LoadOptions class of its own
com.aspose.words.LoadOptions lo = new com.aspose.words.LoadOptions();
lo.setLoadFormat(com.aspose.words.LoadFormat.MHTML);
com.aspose.words.Document doc = new com.aspose.words.Document(
new ByteArrayInputStream(emlStream.toByteArray()), lo);
// Save to disk
doc.save(dataDir + "message.pdf", com.aspose.words.SaveFormat.PDF);
// or save to a stream
ByteArrayOutputStream foStream = new ByteArrayOutputStream();
doc.save(foStream, com.aspose.words.SaveFormat.PDF);
Download Running Code
Download Sample Code
For more details, visit Saving A MSG as PDF.