将 MSG 保存为 PDF

将电子邮件转换为 PDF

以下代码演示了使用 Aspose.Email 与 Aspose.Words for Java 结合将电子邮件转换为 PDF。涉及以下步骤:

  1. 使用 MailMessage 加载电子邮件
  2. 将电子邮件保存为 MemoryStream 且格式为 MHTML
  3. 使用 Aspose.Words 加载流
  4. 将消息保存为 PDF

源电子邮件消息如下所示:

|todo:image_alt_text| | :- | |图示:源 MSG 文件 |

|todo:image_alt_text| | :- | |图示:已转换的 PDF 文件 | 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);
}