ذخیره یک MSG به صورت PDF

تبدیل پیام ایمیل به PDF

کد زیر نشان می‌دهد که چگونه پیام ایمیل را با استفاده از Aspose.Email به همراه Aspose.Words برای جاوا به PDF تبدیل کنید. این کار شامل مراحل زیر است:

  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);
}