Sample code for 30+ languages & platforms
Unicode C

Send HTML Email with Attachments

See more SMTP Examples

Demonstrates how to send an HTML email with file attachments.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkMailManW mailman;
    HCkEmailW email;
    const wchar_t *contentIdStarfish;
    HCkStringBuilderW sbHtml;
    int numReplacements;
    const wchar_t *content;

    success = FALSE;

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

    mailman = CkMailManW_Create();

    CkMailManW_putSmtpHost(mailman,L"smtp.my-starttls-mail-server.com");

    CkMailManW_putSmtpUsername(mailman,L"MY-SMTP-USERNAME");
    CkMailManW_putSmtpPassword(mailman,L"MY-SMTP-PASSWORD");

    CkMailManW_putStartTLS(mailman,TRUE);
    CkMailManW_putSmtpPort(mailman,587);

    // Create a new email object
    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Test SMTP API to Send HTML Email with Attachments");
    CkEmailW_putFrom(email,L"Joe Programmer <joe@my-starttls-mail-server.com>");
    CkEmailW_AddTo(email,L"Chilkat Support",L"support@chilkatsoft.com");

    // Add a plain-text alternative body, which will likely never be seen.
    // (It is shown if the receiving email client is incapable of displaying HTML email.)
    CkEmailW_AddPlainTextAlternativeBody(email,L"This is a plain-text alternative body...");

    // Our HTML will include an image, so add the related image here.
    contentIdStarfish = CkEmailW_addRelatedFile(email,L"qa_data/jpg/starfish.jpg");
    if (CkEmailW_getLastMethodSuccess(email) != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        return;
    }

    // The src attribute for the image tag is set to the contentIdStarfish:
    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_Append(sbHtml,L"<html><body><p>This is an HTML email with an embedded image.</p>");
    CkStringBuilderW_Append(sbHtml,L"<p><img src=\"cid:CONTENT_ID_STARFISH\" /></p></body></html>");
    numReplacements = CkStringBuilderW_Replace(sbHtml,L"CONTENT_ID_STARFISH",contentIdStarfish);

    CkEmailW_AddHtmlAlternativeBody(email,CkStringBuilderW_getAsString(sbHtml));

    // Finally, add some attachments to the email.
    // Add a file attachment.
    success = CkEmailW_AddFileAttachment2(email,L"qa_data/pdf/fishing.pdf",L"application/pdf");
    if (success != TRUE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        CkStringBuilderW_Dispose(sbHtml);
        return;
    }

    // Add an attachment where the content is contained in a string.
    content = L"This is the content of the 2nd attached file.";
    CkEmailW_AddStringAttachment(email,L"someText.txt",content);

    // Send the HTML email.
    success = CkMailManW_SendEmail(mailman,email);
    if (success != TRUE) {
        wprintf(L"%s\n",CkMailManW_lastErrorText(mailman));
        CkMailManW_Dispose(mailman);
        CkEmailW_Dispose(email);
        CkStringBuilderW_Dispose(sbHtml);
        return;
    }

    success = CkMailManW_CloseSmtpConnection(mailman);
    if (success != TRUE) {
        wprintf(L"Connection to SMTP server not closed cleanly.\n");
    }

    wprintf(L"HTML email with attachments sent!\n");


    CkMailManW_Dispose(mailman);
    CkEmailW_Dispose(email);
    CkStringBuilderW_Dispose(sbHtml);

    }