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-C | |
// Load the document into Aspose.Words. | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Test File (docx).docx"); | |
// Save into a memory stream in MHTML format. | |
System::SharedPtr<System::IO::MemoryStream> stream = System::MakeObject<System::IO::MemoryStream>(); | |
doc->Save(stream, SaveFormat::Mhtml); | |
// Rewind the stream to the beginning so Aspose.Email can read it. | |
stream->set_Position(0); | |
// Create an Aspose.Email MIME email message from the stream. | |
System::SharedPtr<Aspose::Email::MailMessage > message = System::MakeObject<Aspose::Email::MailMessage>(); | |
message->Load(stream, System::MakeObject<Aspose::Email::MhtmlLoadOptions>()); | |
message->set_From(u"sender@sender.com"); | |
message->get_To()->Add(u"receiver@gmail.com"); | |
message->set_Subject(u"Aspose.Words + Aspose.Email MHTML Test Message"); | |
// Send the message using Aspose.Email | |
System::SharedPtr<Aspose::Email::Clients::Smtp::SmtpClient> client = System::MakeObject<Aspose::Email::Clients::Smtp::SmtpClient>(); | |
client->set_Host(u"mail.server.com"); | |
client->set_Username(u"username"); | |
client->set_Password(u"password"); | |
client->set_Port(587); | |
client->Send(message); |