Send Email over SSL in Visual C++

In previous section, I introduced how to send email in a simple VC++ project. In this section. 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 Mail.SSL_starttls property. Please see the following example code.

[Visual C++ Exmaple - SSL/TLS]

The following example codes demonstrates how to set SSL/TLS connection.

// Send email by normal TCP/IP without SSL connection
oSmtp->ServerAddr = _T("localhost");
oSmtp->ServerPort = 25;

// Send email by SSL connection with STARTTLS command switching
oSmtp->ServerAddr = _T("localhost");
oSmtp->ServerPort = 25;
oSmtp->SSL_starttls = 1;
oSmtp->SSL_init();

// Send email by SSL connection with direct SSL.
oSmtp->ServerAddr = _T("localhost");
oSmtp->ServerPort = 465;
oSmtp->SSL_starttls = 0;
oSmtp->SSL_init();

// Send email by SSL/TLS connection with auto-detect.
// If port is 25 or 587, STARTTLS TLS will be used; otherwise direct SSL will be used.
oSmtp->ServerAddr = _T("localhost");
oSmtp->ServerPort = 465;
oSmtp->SSL_init();
oSmtp->ServerAddr = _T("localhost");
oSmtp->ServerPort = 25;
oSmtp->SSL_init();

Note

Remarks: All of samples in this section are based on first section: A simple Visual C++ 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.

[Visual 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"
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;

int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize( NULL );
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = _T("TryIt");

    // Set your sender email address
    oSmtp->FromAddr = _T("test@emailarchitect.net");

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 );

    // Set email subject
    oSmtp->Subject = _T("simple email from Visual C++ project");

    // Set email body
    oSmtp->BodyText = _T("this is a test email sent from Visual C++ project, do not reply");

    // Your SMTP server address
    oSmtp->ServerAddr = _T("smtp.emailarchitect.net");

    // User and password for ESMTP authentication, if your server doesn't
    // require User authentication, please remove the following codes.
    oSmtp->UserName = _T("test@emailarchitect.net");
    oSmtp->Password = _T("testpassword");

    // Set SSL 465 port
    oSmtp->ServerPort = 465;

    // Set direct SSL connection
    oSmtp->SSL_starttls = 0;
    oSmtp->SSL_init();

    _tprintf(_T("Start to send email ...\r\n" ));

    if( oSmtp->SendMail() == 0 )
    {
        _tprintf( _T("email was sent successfully!\r\n"));
    }
    else
    {
        _tprintf( _T("failed to send email with the following error: %s\r\n"),
            (const TCHAR*)oSmtp->GetLastErrDescription());
    }
    if( oSmtp != NULL )
        oSmtp.Release();
    return 0;
}

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

The following example codes demonstrate how 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"
#include "easendmailobj.tlh"
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize( NULL );
    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance( "EASendMailObj.Mail");
    oSmtp->LicenseCode = _T("TryIt");

    // Set your sender email address
    oSmtp->FromAddr = _T("test@emailarchitect.net");

    // Add recipient email address
    oSmtp->AddRecipientEx( _T("support@emailarchitect.net"), 0 );

    // Set email subject
    oSmtp->Subject = _T("simple email from Visual C++ project");

    // Set email body
    oSmtp->BodyText = _T("this is a test email sent from Visual C++ project, do not reply");

    // Your SMTP server address
    oSmtp->ServerAddr = _T("smtp.emailarchitect.net");

    // User and password for ESMTP authentication, if your server doesn't
    // require User authentication, please remove the following codes.
    oSmtp->UserName = _T("test@emailarchitect.net");
    oSmtp->Password = _T("testpassword");

    // Set 25 port or 587 port
    oSmtp->ServerPort = 25;
    // Set TLS connection
    oSmtp->SSL_starttls = 1;
    oSmtp->SSL_init();
    _tprintf(_T("Start to send email ...\r\n" ));

    if( oSmtp->SendMail() == 0 )
    {
        _tprintf( _T("email was sent successfully!\r\n"));
    }
    else
    {
        _tprintf( _T("failed to send email with the following error: %s\r\n"),
            (const TCHAR*)oSmtp->GetLastErrDescription());
    }
    if( oSmtp != NULL )
        oSmtp.Release();
    return 0;
}

SMTP Server SSL Certificate

To send email over SSL/TLS connection, you don’t need to install a certificate on your machine. The data is encrypted by server certificate public/private key pair.

SMTP Setting for Gmail, Yahoo, Hotmail and Office 365

Because most popluar email providers support or require SSL/TLS connection, so I will introduce specific setting for Gmail, Yahoo, Hotmail and Office 365 in the coming sections.

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.