Sample code for 30+ languages & platforms
Unicode C

Embed Image in HTML Email

Demonstrates how to create and send an HTML email with an embedded image.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkMailManW.h>
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailManW mailman;
    HCkEmailW email;
    const wchar_t *fileOnDisk;
    const wchar_t *filePathInHtml;
    const wchar_t *htmlBody;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // The mailman object is used for sending and receiving email.
    mailman = CkMailManW_Create();

    // Set the SMTP server.
    CkMailManW_putSmtpHost(mailman,L"smtp.comcast.net");

    // Create a new email object
    email = CkEmailW_Create();

    // Add an embedded image to the HTML email.
    fileOnDisk = L"images/dude2.gif";
    filePathInHtml = L"dudeAbc.gif";

    // Embed the GIF image in the email.
    success = CkEmailW_AddRelatedFile2(email,fileOnDisk,filePathInHtml);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        return;
    }

    // The src attribute for the image tag is set to the filePathInHtml:
    htmlBody = L"<html><body>Embedded Image:<br><img src=\"dudeAbc.gif\"></body></html>";

    // Set the basic email stuff: HTML body, subject, "from", "to"
    CkEmailW_SetHtmlBody(email,htmlBody);
    CkEmailW_putSubject(email,L"Unicode C HTML email with an embedded image.");
    success = CkEmailW_AddTo(email,L"Admin",L"admin@chilkatsoft.com");
    CkEmailW_putFrom(email,L"Chilkat Support <support@chilkatsoft.com>");

    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
    }
    else {
        wprintf(L"Mail Sent!\n");
    }



    CkMailManW_Dispose(mailman);
    CkEmailW_Dispose(email);

    }