
Email Automation is quite popular these days for generating and sending emails automatically from within the web or desktop applications. It is being used for sending important notifications, documents, newsletters, and various kinds of other messages. In order to develop an automated email system, Aspose facilitates the developers with its email API - Aspose.Email. Today, I’m going to pick up the C++ variant of Aspose.Email and show you how to create Outlook emails using C++.
- C++ Email Library
- Create an Outlook Email using C++
- Create an Outlook Email with HTML Body using C++
- Set Encoding for Outlook Email using C++
- Save Outlook Message as Draft using C++
C++ Email Library to Create Outlook Emails
Aspose.Email for C++ is a native C++ library that lets you create and send emails of MS Outlook and other popular email formats. It also allows you to manipulate Outlook objects such as calendars, tasks, contacts, etc. and convert the existing email files to other formats. You can download the complete library package from here or install it within your C++ application using NuGet.
Create an Outlook Email using C++
First of all, let’s check out how to create a simple email message and save it in an email format such as MSG, EML, and EMLX. The following are the steps to create an email message using Aspose.Email for C++.
- Create an instance of MailMessage class.
- Set the message’s properties such as To, From, and Body.
- Save the email message using MailMessage->Save() method.
The following code sample shows how to create an Outlook message in a particular email format using C++.
// Create a new instance of MailMessage class | |
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>(); | |
// Set subject of the message, Html body and sender information | |
message->set_Subject(u"New message created by Aspose.Email for .NET"); | |
message->set_From(System::MakeObject<MailAddress>(L"from@domain.com", L"Sender Name", false)); | |
message->set_Body(System::String(u"This is a plain text.")); | |
// Add TO recipients and Add CC recipients | |
message->get_To()->Add(System::MakeObject<MailAddress>(L"to1@domain.com", L"Recipient 1", false)); | |
message->get_To()->Add(System::MakeObject<MailAddress>(L"to2@domain.com", L"Recipient 2", false)); | |
message->get_CC()->Add(System::MakeObject<MailAddress>(L"cc1@domain.com", L"Recipient 3", false)); | |
message->get_CC()->Add(System::MakeObject<MailAddress>(L"cc2@domain.com", L"Recipient 4", false)); | |
// Save message in EML, EMLX, MSG formats | |
message->Save(u"CreateNewMailMessage_out.eml", SaveOptions::get_DefaultEml()); | |
message->Save(u"CreateNewMailMessage_out.emlx", SaveOptions::CreateSaveOptions(MailMessageSaveType::get_EmlxFormat())); | |
message->Save(u"CreateNewMailMessage_out.msg", SaveOptions::get_DefaultMsgUnicode()); |
Create an Outlook Email with HTML Body using C++
In the previous example, we have created an email with plain text. However, most of the emails these days contain an HTML body for a fine presentation of the email’s content. In such a case, you can configure Aspose.Email and set the HTML-based body of the email. For this, just use MailMessage->set_HtmlBody(System::String) and pass to it the HTML content. The following code sample shows how to create an email with an HTML body using C++.
// Create a new instance of MailMessage class | |
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>(); | |
// Set subject of the message, Html body and sender information | |
message->set_Subject(u"New message created by Aspose.Email for .NET"); | |
message->set_From(System::MakeObject<MailAddress>(L"from@domain.com", L"Sender Name", false)); | |
message->set_HtmlBody(System::String(u"<b>This line is in bold.</b> <br/> <br/>") + u"<font color=blue>This line is in blue color</font>"); | |
// Add TO recipients and Add CC recipients | |
message->get_To()->Add(System::MakeObject<MailAddress>(L"to1@domain.com", L"Recipient 1", false)); | |
message->get_CC()->Add(System::MakeObject<MailAddress>(L"cc1@domain.com", L"Recipient 3", false)); | |
// Save message in EML, EMLX, MSG formats | |
message->Save(u"CreateNewMailMessage_out.emlx", SaveOptions::CreateSaveOptions(MailMessageSaveType::get_EmlxFormat())); | |
message->Save(u"CreateNewMailMessage_out.msg", SaveOptions::get_DefaultMsgUnicode()); |
Set Encoding for Outlook Email using C++
Aspose.Email for C++ also lets you define the encoding standard to guide the browser about how to deal with characters in the email. For setting the particular encoding standard, you can use MailMessage->set_BodyEncoding(System::Text::Encoding) method. The following code sample shows how to create an email with particular encoding using C++.
// Create a new instance of MailMessage class | |
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>(); | |
// Set subject of the message, Html body and sender information | |
message->set_Subject(u"New message created by Aspose.Email for .NET"); | |
message->set_From(System::MakeObject<MailAddress>(L"from@domain.com", L"Sender Name", false)); | |
message->set_Body(System::String(u"This is a plain text.")); | |
message->set_BodyEncoding(System::Text::Encoding::get_ASCII()); | |
// Add TO recipients and Add CC recipients | |
message->get_To()->Add(System::MakeObject<MailAddress>(L"to1@domain.com", L"Recipient 1", false)); | |
message->get_CC()->Add(System::MakeObject<MailAddress>(L"cc1@domain.com", L"Recipient 3", false)); | |
// Save message in EML, EMLX, MSG formats | |
message->Save(u"CreateNewMailMessage_out.eml", SaveOptions::get_DefaultEml()); | |
message->Save(u"CreateNewMailMessage_out.msg", SaveOptions::get_DefaultMsgUnicode()); |
Save Outlook Message as Draft using C++
You can also save the newly created Outlook email message as a draft. The following are the steps to perform this operation:
- Create a new email or load an existing one using the MailMessage object.
- Create a MapiMessage object from the Outlook mail message.
- Use MapiMessage->SetMessageFlags(Aspose::Email::Mapi::MapiMessageFlags) method to set email message as draft.
- Save the Outlook email.
The following code sample shows how to create an email as a draft using C++.
// Create a new instance of MailMessage class | |
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>(); | |
// Set subject of the message, Html body and sender information | |
message->set_Subject(u"New message created by Aspose.Email for .NET"); | |
message->set_From(System::MakeObject<MailAddress>(L"from@domain.com", L"Sender Name", false)); | |
message->set_Body(System::String(u"This is a plain text.")); | |
// Add TO recipients and Add CC recipients | |
message->get_To()->Add(System::MakeObject<MailAddress>(L"to1@domain.com", L"Recipient 1", false)); | |
message->get_CC()->Add(System::MakeObject<MailAddress>(L"cc1@domain.com", L"Recipient 3", false)); | |
// Create an instance of type MapiMessage from MailMessage, Set message flag to un-sent (draft status) and Save it | |
System::SharedPtr<MapiMessage> mapiMsg = MapiMessage::FromMailMessage(message); | |
mapiMsg->SetMessageFlags(Aspose::Email::Mapi::MapiMessageFlags::MSGFLAG_UNSENT); | |
// Save message in EML, EMLX, MSG MHTML formats | |
message->Save(u"CreateNewMailMessage_out.emlx", SaveOptions::CreateSaveOptions(MailMessageSaveType::get_EmlxFormat())); | |
message->Save(u"CreateNewMailMessage_out.msg", SaveOptions::get_DefaultMsgUnicode()); |
Live Demos
Conclusion
In this article, I have shown you how to create Outlook emails within C++ applications. The C++ code samples demonstrated how to create MSG, EML or EMLX emails with text/HTML body or a particular encoding. You can explore more about how to deal with Outlook emails and other items using Aspose.Email for C++.