Send Email with Attachment in Visual C++

In previous section, I introduced how to send HTML email. In this section, I will introduce how to add attachment to email in Visual C++.

Introduction

To send an email with file attachment, we need to use AddAttachment method. This method can attach a file to the email message from local disk or a remote URL.

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 - Add attachment from local disk or remote URL]

The following example codes demonstrate how to send email with file attachments.

Note

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

#include "stdafx.h"
#include <tchar.h>
#include <Windows.h>

#include "EASendMailObj.tlh"
using namespace EASendMailObjLib;

const int ConnectNormal = 0;
const int ConnectSSLAuto = 1;
const int ConnectSTARTTLS = 2;
const int ConnectDirectSSL = 3;
const int ConnectTryTLS = 4;

int _tmain(int argc, _TCHAR* argv[])
{
    ::CoInitialize(NULL);

    IMailPtr oSmtp = NULL;
    oSmtp.CreateInstance(__uuidof(EASendMailObjLib::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("HTML email from Visual C++ project with attachment");

    // Set HTML body format
    oSmtp->BodyFormat = 1;
    // Set HTML body text
    oSmtp->BodyText = _T("<font size=5>This is</font> <font color=red><b>a test</b></font>");

    // Add attachment from local disk
    if(oSmtp->AddAttachment(_T("c:\\test.doc")) != 0)
    {
        _tprintf(_T("Failed to add attachment with error: %s\r\n"),
             (const TCHAR*)oSmtp->GetLastErrDescription());
    }

    // Add attachment from remote website
    if(oSmtp->AddAttachment("http://www.emailarchitect.net/webapp/img/logo.jpg") != 0)
    {
        _tprintf(_T("Failed to add attachment with error: %s\r\n"),
             (const TCHAR*)oSmtp->GetLastErrDescription());
    }

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

    // Most mordern SMTP servers require SSL/TLS connection now.
    // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
    oSmtp->ConnectType = ConnectTryTLS;

    // If your SMTP server uses 587 port
    // oSmtp->ServerPort = 587;

    // If your SMTP server requires SSL/TLS connection on 25/587/465 port
    // oSmtp->ServerPort = 25; // 25 or 587 or 465
    // oSmtp->ConnectType = ConnectSSLAuto;

    _tprintf(_T("Start to send HTML 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());
    }

    return 0;
}

Next Section

At next section I will introduce how to add embedded images/pictures to email message.

Appendix

Comments

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