EASendMail SMTP Component > Developer Center > Send Email in Managed C++ - Tutorial

Send Email in Managed C++ - Tutorial

Introduction

EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). This tutorial covers everything of sending email with EASendMail in Managed C++.

Installation

Before you can use the following sample codes, you should download the EASendMail Installer and install it on your machine at first.

A simple Managed C++ project

To better demonstrate how to use EASendMail sending email, let's create a Managed C++ console project at first, and then add the reference of EASendMail in your project.

Managed C++ console project

Add Reference of EASendMail to Visual Stuido Managed C++ Project

To use EASendMail SMTP Component in your project, the first step is "Add reference of EASendMail to your project". Please create/open your project with Visual Studio.NET, then choose menu->"Project"->"Add Reference"->".NET"->"Browse...", and choose the EASendMail{version}.dll from your disk, click "Open"->"OK", the reference of EASendMail will be added to your project, and you can start to use EASendMail to send email in your project.

add reference in Managed C++

Because EASendMail has separate builds for .Net Framework, please refer to the following table and choose the correct dll.

Separate builds of run-time assembly for .Net Framework 1.1, 2.0, 3.5, 4.0 and .Net Compact Framework 2.0, 3.5.

File .NET Framework Version
EASendMail.dll Built with .NET Framework 1.1
It requires .NET Framework 1.1, 2.0, 3.5 or later version.
EASendMail20.dll Built with .NET Framework 2.0
It requires .NET Framework 2.0, 3.5 or later version.
EASendMail35.dll Built with .NET Framework 3.5
It requires .NET Framework 3.5 or later version.
EASendMaill40.dll Built with .NET Framework 4.0
It requires .NET Framework 4.0 or later version.
EASendMailCF20.dll Built with .NET Compact Framework 2.0
It requires .NET Compact Framework 2.0, 3.5 or later version.
EASendMailCF35.dll Built with .NET Compact Framework 3.5
It requires .NET Compact Framework 3.5 or later version.

[Managed C++ Example - Send email]

Now add the following codes to the project and change From, To, Server, User and Password to corresponding value.

#include "stdafx.h"

using namespace System;
using namespace EASendMail;

int main(array<System::String ^> ^args)
{
    SmtpMail ^oMail = gcnew SmtpMail("TryIt");
    SmtpClient ^oSmtp = gcnew SmtpClient();

    // Set sender email address, please change it to yours
    oMail->From = "test@emailarchitect.net";

    // Set recipient email address, please change it to yours
    oMail->To = "support@emailarchitect.net";
    
    // Set email subject
    oMail->Subject = "test email from Managed C++ project";
    
    // Set email body
    oMail->TextBody = "this is a test email sent from Managed C++ project, do not reply";

    // Your SMTP server address
    SmtpServer ^oServer = gcnew SmtpServer("smtp.emailarchitect.net");
    
    // User and password for ESMTP authentication, if your server doesn't require
    // User authentication, please remove the following codes.           
    oServer->User = "test@emailarchitect.net";
    oServer->Password = "testpassword";

    // If your smtp server requires SSL connection, please add this line
    // oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;

    try
    {
        Console::WriteLine("start to send email from Managed C++...");
        oSmtp->SendMail(oServer, oMail);
        Console::WriteLine("email was sent successfully!");
    }
    catch (Exception ^ep)
    {
        Console::WriteLine("failed to send email with the following error:");
        Console::WriteLine(ep->Message);
    }

    return 0;
}

If you set everything right, you can get "email was sent successfully". If you get "failed to send email with the following error:", then please have a look at the following section.

Where can I get my SMTP email server address, user and password?

Because each email account provider has different server address, so you should query your SMTP server address from your email account provider. To prevent spreading email from the server, most SMTP servers also require user authentication. User name is your email address or your email address without domain part, it depends on your email provider setting.

When you execute above example code, if you get error about "Networking connection" or "No such host", it is likely that your SMTP server address is not correct. If you get an error like "5xx Relay denied", it is likely that you did not set user authentication. Another common error is "5xx Must issue a STARTTLS command first" or "No supported authentication marshal found!", that is because your SMTP server requires user authentication under SSL connection. You can set the SSL connection to solve this problem.

Finally, if you have already set your account in your email client such as Outlook or Window Mail, you can query your SMTP server address, user in your email client. For example, you can choose menu -> "Tools" - > - "Accounts" - > "Your email account" - > "Properties" - > "Servers" in Outlook express or Windows Mail to get your SMTP server, user. Using EASendMail to send email does not require you have email client installed on your machine or MAPI, however you can query your exist email accounts in your email client.

Managed C++ console email sample

Email Address Syntax and Multiple Recipients

Mail Address Syntax in EASendMail SMTP Component:

For single email address (From, ReplyTo, ReturnPath), the syntax can be: ["][display name]["]<email address>.
For example: "Tester, T" <test@adminsystem.com>, Tester <test@adminsystem.com>, <test@adminsystem.com> or test@adminsystem.com.

For mulitple email address (To, CC, Bcc), the syntax can be: [single email],[single email]... (,;\r\n) can be used to separate multiple email addresses.
For example: "Tester, T" <test1@adminsystem.com>, Tester2 <test2@adminsystem.com>, <test3@adminsystem.com>, test4@adminsystem.com

[Managed C++ Example - Email syntax]

To better understand the email address syntax, please refer to the following codes.

// From is a MailAddress object, it supports implicit converting from string.
// The syntax is like this: "test@adminsystem.com" or "Tester<test@adminsystem.com>"
// The example code without implicit converting.
oMail->From = gcnew MailAddress( "Tester", "test@adminsystem.com" ); 
oMail->From = gcnew MailAddress( "Tester<test@adminsystem.com>"); 
oMail=>From = gcnew MailAddress( "test@adminsystem.com" ); 

// To, Cc and Bcc is a AddressCollection object, it supports implicit converting
// from string. Multiple addresses are separated with (,;) 
// The syntax is like this: "test@adminsystem.com, test1@adminsystem.com" 
// The example code without implicit converting 
oMail->To = gcnew AddressCollection( "test1@adminsystem.com, test2@adminsystem.com" ); 
oMail->To = gcnew AddressCollection( "Test1<test@adminsystem.com>, Test2<test2@adminsystem.com>");

// You can add more recipient by Add method 
oMail->To->Add( gcnew MailAddress( "tester", "test@adminsystem.com"));

// You can also add  carbon copy (CC) or blind carbon copy (BCC) in the email.
oMail->Cc->Add( gcnew MailAddress( "CC recipient", "cc@adminsystem.com"));
oMail->Bcc->Add( gcnew MailAddress( "Bcc recipient", "bcc@adminsystem.com"));

Reply-To, Return-Path and Mail Priority

If you want to set another email address to get the replied email rather than your From address, you can use ReplyTo property.

If you want to set another email address to get the delivery report rather than your From address, you can use ReturnPath property.

If you want to set Higher or Lower priority to your email, you can use Priority prority

[Managed C++ Example - ReplyTo, ReturnPath and Priority]

oMail->From = "Tester <test@emailarchitect.net>";

// Set the Reply-To address
oMail->ReplyTo = "replyto@@emailarchitect.net";

// Set the email address to receive delivery report
oMail->ReturnPath = "report@emailarchitect.net";

// Set high priority
oMail->Priority = MailPriority::High;
Next Section

In this section, I introduced the basic things of sending email in Managed C++ with EASendMail. At next section I will introduce how to send email over SSL connection.

Next: Send email over SSL in Managed C++ ->

Comments

If you have any comments or questions about above example codes, please click here to add your comments.

Download

Send Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Retrieve Email and Parse Email - C# - VB6 - Visual Basic - VC++ - Managed C++ - Delphi

Email Solution - Email Server - DomainKeys/DKIM - Disclaimer

2003 - 2011 © Copyright AdminSystem Software Limited. All rights reserved.   About us     Follow emailarchitect on Twitter