Convert a Document to MHTML and Send It by Email
Aspose.Words can convert any document to the MHTML (Web Archive) format. This makes it convenient to use Aspose.Words and Aspose.Email
together. You can load a predefined document in any supported format, such as DOC, OOXML, or RTF, into Aspose.Words, fill it with data, save the resulting document as MHTML, and then send it by e-mail using Aspose.Email
.
The following code example shows how to convert any document to MHTML and send it by 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); |