Send Word Document in Email Body using C++

Send Word Document in Email Body using C++

Emails are the primary source of communication over the internet and in the majority of cases, the layout and formatting of the email is important. However, most of the email clients do not provide the enhanced formatting options required for designing high-quality email content. In such cases, a well-formatted Word document can be used as the body of the email. In this article, you will learn how to send a Word document as the body of the email using C++.

C++ APIs for Sending Word Documents In Email Body

In order to send a Word document in an email body, we will use the Aspose.Words for C++ and Aspose.Email for C++ APIs. The former allows you to generate, modify and convert Microsoft Word files. Whereas the latter allows you to create, manipulate, and convert Outlook files. We will use Aspose.Words for C++ API to convert the Word document to MHTML format and Aspose.Email for C++ API to generate and send the email. You can either install the APIs through NuGet or download them directly from the Downloads section.

PM> Install-Package Aspose.Words.Cpp
PM> Install-Package Aspose.Email.Cpp

Sending a Word Document in an Email Body using C++

The following are the steps to send a Word document in an email body:

The following sample code shows how to send a Word document as the body of an email using C++.

// Directory paths.
System::String sourceDataDir = u"SourceDirectory\\";
System::String outputDataDir = u"OutputDirectory\\";
// Load the Word document using Aspose.Words Document class.
System::SharedPtr<Aspose::Words::Document> doc = System::MakeObject<Aspose::Words::Document>(sourceDataDir + u"Sample 1.docx");
// Save the document into the MemoryStream 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 instance of the MailMessage class
System::SharedPtr<Aspose::Email::MailMessage> message = MailMessage::Load(stream, System::MakeObject<Aspose::Email::MhtmlLoadOptions>());
// Set From Email Address
message->set_From(u"your_from@email.com");
// Set To Email Address
message->set_To(u"your_to@email.com");
// Set Email Subject
message->set_Subject(u"Aspose.Words + Aspose.Email MHTML Test Message");
// Create an instance of the SmtpClient class
System::SharedPtr<Aspose::Email::Clients::Smtp::SmtpClient> client = System::MakeObject<Aspose::Email::Clients::Smtp::SmtpClient>();
// Set the Host e.g: smtp.gmail.com
client->set_Host(u"your_smtp.com");
// Set the Email Address
client->set_Username(u"your_email@email.com");
// Set the Password
client->set_Password(u"your_password");
// Set the Port
client->set_Port(587);
// Set the Security Options
client->set_SecurityOptions(SecurityOptions::SSLExplicit);
// Send the Email Message
client->Send(message);

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to send a Word document in an email body using C++. To summarize, you learned how to convert a Word document to MHTML format using Aspose.Words for C++ API and how to send MHTML as an email using Aspose.Email for C++ API. Both of these APIs provide numerous features for working with MS Word and email files. You can explore these APIs in detail by visiting their official documentation. In case of any questions, please feel free to reach us on our free support forum.

See Also