Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

Add an attachment and an inline image

See more Email Object Examples

Add a file attachment from memory and embed an image in the HTML body using its Content-ID. Start with any email that has an HTML body, or use text-and-html.eml from the preceding example. Save the result as with-attachments.eml.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkEmailW.h>
#include <CkBinDataW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkEmailW email;
    success = email.LoadEml(L"text-and-html.eml");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    CkBinDataW data;
    success = data.AppendString(L"Time,Activity\r\n09:00,Welcome\r\n10:00,Field session\r\n",L"utf-8");
    if (success != true) {
        wprintf(L"Failed: data.AppendString.\n");
        return;
    }

    success = email.AddAttachmentBd(L"agenda.csv",data,L"text/csv");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    //  AddRelatedBd returns the identifier that must appear after cid: in the HTML.
    CkBinDataW image;
    success = image.AppendEncoded(L"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAAASUlEQVR4nGMQK/ImBv3//x/CYCBSNQkaIKqJ1YCmGkUDsigu1egakOWwqsaiAaICl2p0P8DV4VKNxdP4VWMPJTyqiY04sZGuAQBhyhp6A/ap2AAAAABJRU5ErkJggg==",L"base64");
    if (success != true) {
        wprintf(L"Failed: image.AppendEncoded.\n");
        return;
    }

    const wchar_t *cid = email.addRelatedBd(L"confirmed.png",image);
    if (email.get_LastMethodSuccess() != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    CkStringBuilderW html;
    html.Append(L"<p><img alt='Confirmed' src=\"cid:");
    html.Append(cid);
    html.Append(L"\"></p>");
    html.Append(email.getHtmlBody());
    success = email.AddHtmlAlternativeBody(html.getAsString());
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    success = email.SaveEml(L"with-attachments.eml");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    wprintf(L"Saved with-attachments.eml.\n");
    }