Запазване на MSG като PDF

Конвертиране на имейл съобщение към PDF

Следният код показва конвертиране на имейл съобщение към PDF с помощта на Aspose.Email в комбинация с Aspose.Words за Java. Това включва следните стъпки:

  1. Заредете имейл съобщението, използвайки MailMessage
  2. Запазете имейл съобщението в MemoryStream като MHTML
  3. Заредете потока с помощта на Aspose.Words
  4. Запазете съобщението като PDF

Изходното имейл съобщение може да се види по следния начин:

|todo:image_alt_text| | :- | |Figure: Source MSG File |

|todo:image_alt_text| | :- | |Figure: Converted PDF File | Java

static void emailToPdf(String emailPath, String pdfPath) throws Exception {
    MailMessage eml = MailMessage.load(emailPath);

    // Save the message to an 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(pdfPath, com.aspose.words.SaveFormat.PDF);

    // or save to a stream
    ByteArrayOutputStream foStream = new ByteArrayOutputStream();
    doc.save(foStream, com.aspose.words.SaveFormat.PDF);
}