Send Email over SSL in Managed C++/CLI

In previous section, I introduced how to send email in a simple Managed C++/CLI project. In this section, I will introduce the SSL connection.

Introduction

SSL and TLS

SSL connection encrypts data between the SMTP component and SMTP server to protects user, password and email content in TCP/IP level. Now this technology is commonly used and many SMTP servers are deployed with SSL such as Gmail, Yahoo and Hotmail. There are two ways to deploy SSL on SMTP server:

  • Explicit SSL (TLS)

    Using STARTTLS command to switch SSL channel on normal SMTP port (25 or 587);

  • Implicit SSL

    Deploying SSL on another port (465 or other port, you may query it from your server administrator

EASendMail SMTP component supports both ways. The connection can be specified by EASendMail.SmtpConnectType enumeration. Please see the following example code.

TLS 1.2

TLS is the successor of SSL, EASendMail supports SSL 3.0/TLS1.0 - 1.2 very well. In EASendMail, ConnectSTARTTLS doesn’t mean TLS encryption, it means STARTTLS command in SMTP protocol.

You don’t have to set any property to enable TLS 1.2 encryption. If your server requires TLS 1.2 encryption, TLS 1.2 encryption is used automatically.

To enable TLS 1.2 on Windows 2008/2008 R2/7, please install this update:

TLS 1.2 Support added to Windows Server 2008

[Managed C++/CLI Exmaple - SSL/TLS]

// Send email by normal TCP/IP without SSL connection
SmtpServer^ oServer = gcnew SmtpServer("localhost 25");
oServer->ConnectType = SmtpConnectType::ConnectNormal;

// Send email by SSL connection with STARTTLS command switching
SmtpServer^ oServer = gcnew SmtpServer("localhost 25");
oServer->ConnectType = SmtpConnectType::ConnectSTARTTLS;

// Send email by SSL connection with direct SSL.
SmtpServer^ oServer = gcnew SmtpServer("localhost 465");
oServer->ConnectType = SmtpConnectType::ConnectDirectSSL;

// Send email by SSL connection with auto-detect.
// If port is 25 or 587, STARTTLS SSL will be used; otherwise direct SSL will be used.
SmtpServer^ oServer = gcnew SmtpServer("localhost 465");
oServer->ConnectType = SmtpConnectType::ConnectSSLAuto
SmtpServer^ oServer = gcnew SmtpServer("localhost 25");
oServer->ConnectType = SmtpConnectType::ConnectSSLAuto;

Note

Remarks: All of samples in this section are based on first section: A simple Managed C++/CLI project. To compile and run the following example codes successfully, please click here to learn how to create the test project and add reference of EASendMail to your project.

[Managed C++ Example - Send email over direct SSL on 465 port]

The following example codes demonstrate how to send email with direct SSL connection on 465 port.

Note

To get the full sample projects, please refer to Samples section.

#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");

    // Set SSL 465 port
    oServer->Port = 465;

    // Set direct SSL connection
    oServer->ConnectType = SmtpConnectType::ConnectDirectSSL;

    // 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";

    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;
}

[Managed C++ Example - Send email over TLS on 25 or 587 port]

The following example codes demonstrate how to use EASendMail SMTP component to send email with TLS (STARTTLS command) connection on 25 port.

Note

To get the full sample projects, please refer to Samples section.

#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");

    // Set 25 port
    oServer->Port = 25;

    // detect TLS/SSL automatically
    oServer->ConnectType = SmtpConnectType::ConnectDirectSSL;

    // 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";

    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;
}

Next Section

At next section I will introduce how to send email using Gmail account.

Appendix

Comments

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