Sample code for 30+ languages & platforms
Unicode C

Send HTML Email with CSS as Related Item using Content-Location

See more SMTP Examples

Demonstrates how to compose an HTML email with an external CSS file included as a related item and referenced by Content-Location.

Some email clients display embedded (related) content best using CID's (Content-IDs), whereas other email clients display related content best by Content-Location. The choice you make may depend on the software used by the intended recipient of your email. (Does' the recipient read email on an iPhone? Android? Outlook? GMail? Thunderbird? etc.)

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;
    HCkStringBuilderW sbCss;
    BOOL bCrlf;
    const wchar_t *filenameInHtml;
    HCkStringBuilderW sbHtml;

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

    // Use your SMTP server hostname.  This example uses office365, but it could be any SMTP server..
    CkMailManW_putSmtpHost(mailman,L"outlook.office365.com");
    CkMailManW_putSmtpPort(mailman,587);
    CkMailManW_putStartTLS(mailman,TRUE);

    // Set the SMTP login/password
    CkMailManW_putSmtpUsername(mailman,L"OFFICE365-SMTP-LOGIN");
    CkMailManW_putSmtpPassword(mailman,L"OFFICE365-SMTP-PASSWORD");

    // Create a new email object
    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"HTML Email with embedded CSS");
    CkEmailW_putFrom(email,L"Chilkat Support <my-office365-user@mydomain.com>");
    CkEmailW_AddTo(email,L"Chilkat Support",L"support@chilkatsoft.com");

    sbCss = CkStringBuilderW_Create();
    bCrlf = TRUE;
    CkStringBuilderW_AppendLine(sbCss,L"body {",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"    background-color: powderblue;",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"h1 {",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"    color: blue;",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"p {",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"    color: red;",bCrlf);
    CkStringBuilderW_AppendLine(sbCss,L"}",bCrlf);

    // The filenameInHtml is what should exists within the HTML (in the href atribute)
    filenameInHtml = L"styles.css";
    // Call AddRelatedString2 to use Content-Location.
    CkEmailW_AddRelatedString2(email,filenameInHtml,CkStringBuilderW_getAsString(sbCss),L"utf-8");

    sbHtml = CkStringBuilderW_Create();
    CkStringBuilderW_AppendLine(sbHtml,L"<!DOCTYPE html>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"<html>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"<head>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"  <link rel=\"stylesheet\" href=\"styles.css\">",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"</head>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"<body>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"<h1>This is a heading</h1>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"<p>This is a paragraph.</p>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"</body>",bCrlf);
    CkStringBuilderW_AppendLine(sbHtml,L"</html>",bCrlf);

    CkEmailW_SetHtmlBody(email,CkStringBuilderW_getAsString(sbHtml));

    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);
    CkStringBuilderW_Dispose(sbCss);
    CkStringBuilderW_Dispose(sbHtml);

    }