Visual C++ - DomainKeys and DKIM Signature

The following c++ example codes demonstrate how to add DomainKeys and DKIM signature to email and send it using SMTP server.

DKIM and DomainKeys

DomainKeys is a technology proposal that can bring black and white back to this decision process by giving email providers a mechanism for verifying both the domain of each email sender and the integrity of the messages sent. To learn more detail about DomainKeys, please refer to

DKIM is a similar technology as Domainkeys. To learn more detail about DKIM, please refer to RFC4871.

Before you can add DomainKeys and DKIM signature to email, you need to generate the certificate and X-DK-File like this:

Download Certificate for your DomainKeys/DKIM key pair

Please click here to go to this online tool at first. Then input your email sender domain in it and click “Download Certificate” and save the downloaded *.pfx file to your local disk. This file contains your DomainKeys and DKIM key pair.

To sign the DomainKeys with EASendMail, we also need to create a text file with notepad and save it.

For example: We create a emailarchitectdomainkeys.txt and contains the following content:

dk_s: s1024
PFXPath: c:\my cert\emailarchitect.pfx
PFXPassword: mypassword
DKIM: yes
DomainKey: yes

// dk_d is the email sender domain,
// dk_s is the domain public key selector.
// PFXPath is the PFX file full path.
// PFXPassword is the PFX file password.

We also need to deploy the certificate Public Key to your domain DNS server. You can export the public key by the online tool:

Deploy Public Key

Once you get the public key, you should set a TXT record in your domain DNS server. For example: your selector is s1024, your domain is, and then you should create a TXT record for and set the following content in the record.      text =

        "t=y; k=rsa; p=MIGfMA0GCSqGS...hSinuQIDAQAB"

To learn more detail about Public Key deployment, please refer to


EASendMail is a SMTP component which supports all operations of SMTP/ESMTP protocols (RFC 821, RFC 822, RFC 2554). Before you can use the following example codes, you should download the EASendMail Installer and install it on your machine at first.

Add Reference

To use EASendMail SMTP ActiveX Object in your C++ project, the first step is “Add header files of EASendMail to your project”. Please go to C:\Program Files\EASendMail\Samples_VC\simple.vcNative or C:\Program Files (x86)\EASendMail\Samples_VC\simple.vcNative folder, find easendmailobj.tlh and easendmailobj.tli, and then copy these files to your project folder.

add reference in Visual C++

C++ - DomainKeys and DKIM Signature - Example

The following example codes demonstrate sending email with DomainKeys and DKIM signature. In order to run it correctly, please change SMTP server, user, password, sender, recipient value to yours.


To get full sample projects, please download and install EASendMail on your machine.

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

    // Add recipient email address
    oSmtp->AddRecipientEx( _T(""), 0 );

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

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

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

    // If your SMTP server requires SSL connection, please add this line
    // oSmtp->SSL_init();

    // Add DomainKeys and DKIM configuration file
    // Then this email will be signed automatically
    oSmtp->AddHeader( _T("X-DK-File"), _T("c:\\emailarchitectdomainkeys.txt"));

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

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

    if( oSmtp != NULL )

    return 0;


If your SMTP server has implemented DKIM, you don’t have to sign DKIM in your codes, your email will be signed by your server automatically.

32bit/x64 ActiveX DLL

Seperate builds of run-time dll for 32 and x64 platform

File Platform
Installation Path\EASendMailObj.dll 32 bit
Installation Path\x64\EASendMailObj.dll 64 bit


  • Standard EXE

    For VB6, C++, Delphi or other standard exe application, you can distribute EASendMailObj.dll with your application to target machine without COM-registration and installer. To learn more detail, please have a look at Registration-free COM with Manifest File.

  • Script

    For ASP, VBScript, VBA, MS SQL Stored Procedure, you need to install EASendMail on target machine by EASendMail installer, both 32bit/x64 DLL are installed and registered.



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