Chuyển đổi một Tài liệu sang MHTML và Gửi qua Email
Aspose.Words có thể chuyển đổi bất kỳ tài liệu nào sang định dạng MHTML (Lưu trữ Web). Điều này khiến việc sử dụng Aspose.Words và Aspose.Email
cùng nhau trở nên tiện lợi. Bạn có thể tải một tài liệu đã định sẵn trong bất kỳ định dạng nào được hỗ trợ như DOC, OOXML, hoặc RTF vào Aspose.Words, điền nó bằng dữ liệu, lưu tài liệu kết quả dưới dạng MHTML, và sau đó gửi nó qua email bằng cách sử dụng Aspose.Email
.
Mã ví dụ sau cho thấy cách chuyển đổi bất kỳ tài liệu nào thành MHTML và gửi nó qua email:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java | |
// Load the document | |
Document doc = new Document(dataDir + "Document.doc"); | |
// Save to an output stream in MHTML format. | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
doc.save(outputStream, SaveFormat.MHTML); | |
// Load the MHTML stream back into an input stream for use with Aspose.Email. | |
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); | |
// Create an Aspose.Email MIME email message from the stream. | |
MailMessage message = MailMessage.load(inputStream); | |
message.setFrom(new MailAddress("your_from@email.com")); | |
message.getTo().add("your_to@email.com"); | |
message.setSubject("Aspose.Words + Aspose.Email MHTML Test Message"); | |
// Save the message in Outlook MSG format. | |
message.save(dataDir + "Message Out.msg", SaveOptions.getDefaultMsg()); | |
// Send the message using Aspose.Email | |
SmtpClient client = new SmtpClient(); | |
client.setHost("your_smtp.com"); | |
client.send(message); |