Lưu tin nhắn email thành PDF

Aspose.Email - Lưu tin nhắn email dưới dạng PDF

Đoạn mã dưới đây cho thấy cách chuyển tin email sang PDF bằng Aspose.Email kết hợp với Aspose.Words cho Java. Điều này bao gồm các bước sau:

  1. Tải tin email bằng MailMessage
  2. Lưu tin email vào MemoryStream dưới dạng MHTML
  3. Tải luồng bằng Aspose.Words
  4. Lưu tin dưới dạng 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);

Tải mã đang chạy

Tải mã mẫu