Send Email using Gmail in Visual C++

In previous section, I introduced how to send email over SSL connection. In this section, I will introduce how to use your Gmail account to send email in Visual C++.

Introduction

Gmail SMTP server address is smtp.gmail.com. It requires implicit SSL or explicit SSL (TLS) connection, and you should use your Gmail email address as the user name for ESMTP authentication. For example: your email is gmailid@gmail.com, and then the user name should be gmailid@gmail.com.

Server Port SSL/TLS
smtp.gmail.com 25, 587 TLS
smtp.gmail.com 465 SSL

Important

If you got authentication error, you need to enable Allowing less secure apps or Sign in using App Passwords.

Another way is Gmail OAUH, please see Gmail SMTP OAUTH section.

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 using Gmail account over Implicit SSL connection on 465 port]

The following example codes demonstrate how to send email using Gmail account.

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 gmail email address
    oSmtp->FromAddr = _T("gmailid@gmail.com");

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

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

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

    // Gmail SMTP server address
    oSmtp->ServerAddr = _T("smtp.gmail.com");

    // Use SSL 465 port,
    oSmtp->ServerPort = 465;

    // detect SSL/TLS automatically
    oSmtp->SSL_init();

    // Gmail user authentication should use your
    // Gmail email address as the user name.
    // For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp->UserName = _T("gmailid@gmail.com");
    oSmtp->Password = _T("yourpassword");
    _tprintf(_T("Start to send email via gmail account ...\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 using Gmail account over TLS connection on 587 port]

The following example codes demonstrate how to send email using Gmail account. To get the full samples of EASendMail, 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 gmail email address
    oSmtp->FromAddr = _T("gmailid@gmail.com");

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

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

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

    // Gmail SMTP server address
    oSmtp->ServerAddr = _T("smtp.gmail.com");

    // Use TLS 587 port,
    oSmtp->ServerPort = 587;

    // detect SSL/TLS automatically
    oSmtp->SSL_init();

    // Gmail user authentication should use your
    // Gmail email address as the user name.
    // For example: your email is "gmailid@gmail.com", then the user should be "gmailid@gmail.com"
    oSmtp->UserName = _T("gmailid@gmail.com");
    oSmtp->Password = _T("yourpassword");
    _tprintf(_T("Start to send email via gmail account ...\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;
}

Gmail SMTP OAUTH

The Gmail IMAP and SMTP servers have been extended to support authorization via the industry-standard OAuth 2.0 protocol. Using OAUTH protocol, user can do authentication by Gmail Web OAuth instead of inputting user and password directly in application. This way is more secure, but a little bit complex.

Using Gmail SMTP OAUTH

Next Section

At next section I will introduce how to send email with Yahoo account.

Appendix

Comments

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