Send Outlook Emails in C++

In the previous article, you have seen how to create MS Outlook emails including MSG, EML, and EMLX programmatically using C++. In this post, you’ll learn how to send Outlook emails from within C++ applications. The emails can be created on the runtime or loaded from already saved email files such as .msg, .emlx, or etc.

Send Emails with C++ Email Library - Free Download

Likewise the previous post, we’ll use Aspose.Email for C++ to send the Outlook emails. You can download the library files from the Downloads section or install it using NuGet.

Send Outlook Emails using C++

The following are the steps to send an Outlook email with SMTP client using Aspose.Email for C++.

  • Create an Outlook email or load it from a file using the MailMessage class.
  • Create an object of SmtpClient.
  • Set host, username, password, and port number.
  • Set security options.
  • Send email using SmtpClient->Send() method.

The following code sample shows how to send an Outlook email using C++.

// Create a new instance of MailMessage class
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>();
System::SharedPtr<SmtpClient> client = System::MakeObject<SmtpClient>();
// 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"<b>This line is in bold.</b> <br/> <br/>") + u"<font color=blue>This line is in blue color</font>");
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_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));
// Specify your mailing host server, Username, Password, Port # and Security option
client->set_Host(u"mail.server.com");
client->set_Username(u"username");
client->set_Password(u"password");
client->set_Port(587);
client->set_SecurityOptions(Aspose::Email::Clients::SecurityOptions::SSLExplicit);
try
{
// Send this message
client->Send(message);
}
catch (System::Exception& ex)
{
}

C++ Send Outlook Emails with Alternate View

You may also specify an alternate view of the email to create a copy of the message in a different format. For instance, if your message is in HTML format, you can create an alternate view having a plain text. In order to create an alternate view, you can use MailMessage->get_AlternateViews()->Add(AlternateView::CreateAlternateViewFromString(u"text")) method.

The following code sample shows how to send an email with an alternate view using C++.

// Create an instance of MailMessage class
System::SharedPtr<MailMessage> message = System::MakeObject<MailMessage>();
// From and To field
message->set_From(u"sender@sender.com");
message->get_To()->Add(u"receiver@receiver.com");
System::SharedPtr<AlternateView> alternate;
// Create an instance of AlternateView to view an email message using the content specified in the string
alternate = AlternateView::CreateAlternateViewFromString(u"This is the alternate Text");
// Add alternate text
message->get_AlternateViews()->Add(alternate);
// Create an instance of SmtpClient Class
System::SharedPtr<SmtpClient> client = System::MakeObject<SmtpClient>();
// Specify your mailing host server, user name, mail password and Port #
client->set_Host(u"smtp.server.com");
client->set_Username(u"Username");
client->set_Password(u"Password");
client->set_Port(25);
try
{
// Client->Send will send this message
client->Send(message);
}
catch (System::Exception& ex)
{
System::Diagnostics::Trace::WriteLine(System::ObjectExt::ToString(ex));
}

Send Bulk Emails using C++

There might be the case when you need to send a bulk of emails at a time. For such cases, Aspose.Email for C++ provides MailMessageCollection class to encapsulate multiple email messages. The following are steps to send the bulk of emails.

  • Create or load email messages using the MailMessage class.
  • Create an object of the MailMessageCollection class.
  • Add email messages to the collection using MailMessageCollection->add() method.
  • Create an object of the SmtpClient class.
  • Send bulk emails using SmtpClient->Send(MailMessageCollection) method.

The following code sample shows how to send the bulk of emails using C++.

// Create SmtpClient as client and specify server, port, user name and password
System::SharedPtr<SmtpClient> client = System::MakeObject<SmtpClient>(u"mail.server.com", 25, u"Username", u"Password");
// Create instances of MailMessage class and Specify To, From, Subject and Message
System::SharedPtr<MailMessage> message1 = System::MakeObject<MailMessage>(u"msg1@from.com", u"msg1@to.com", u"Subject1", u"message1, how are you?");
System::SharedPtr<MailMessage> message2 = System::MakeObject<MailMessage>(u"msg1@from.com", u"msg2@to.com", u"Subject2", u"message2, how are you?");
System::SharedPtr<MailMessage> message3 = System::MakeObject<MailMessage>(u"msg1@from.com", u"msg3@to.com", u"Subject3", u"message3, how are you?");
// Create an instance of MailMessageCollection class
System::SharedPtr<MailMessageCollection> manyMsg = System::MakeObject<MailMessageCollection>();
manyMsg->Add(message1);
manyMsg->Add(message2);
manyMsg->Add(message3);
// Bulk send
try
{
// Send Messages
client->Send(manyMsg);
}
catch (System::Exception& ex)
{
System::Diagnostics::Trace::WriteLine(System::ObjectExt::ToString(ex));
}

C++ Send Emails as TNEF

MS Outlook uses Transport Neutral Encapsulation Format (TNEF) to send the emails having RTF bodies. In this case, the formatting is extracted from the email and it is encoded as TNEF. At the receiving end, if the client supports TNEF, it assembles the plain text and the TNEF attachment to create the RTF email. Otherwise, the email is displayed as plain text. In order to send emails as TNEF, you can use SmtpClient->set_UseTnef(bool) method.

The following code sample shows how to send an Outlook email as TNEF using C++.

try
{
// Email file path
System::String emlFileName = u"Message.eml";
// A TNEF Email
// Load from eml
System::SharedPtr<MailMessage> eml1 = MailMessage::Load(emlFileName, System::MakeObject<EmlLoadOptions>());
eml1->set_From(u"somename@gmail.com");
eml1->get_To()->Clear();
eml1->get_To()->Add(System::MakeObject<MailAddress>(u"first.last@test.com"));
eml1->set_Subject(u"With PreserveTnef flag during loading");
eml1->set_Date(System::DateTime::get_Now());
System::SharedPtr<SmtpClient> client = System::MakeObject<SmtpClient>(u"smtp.gmail.com", 587, u"somename", u"password");
client->set_SecurityOptions(Aspose::Email::Clients::SecurityOptions::Auto);
client->set_UseTnef(true);
// Use this flag to send as TNEF
client->Send(eml1);
}
catch (System::Exception& ex)
{
// catch exception
}

Conclusion

In this post, you have seen how to send Outlook emails using C++. In addition, you have come to know how to send bulk emails, send emails with an alternate view, or send emails as TNEF within C++ applications. You can explore more about Aspose’s C++ email library using the documentation.

See Also