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

Create an email with plain-text and HTML bodies

See more Email Object Examples

Set the From and Reply-To addresses, add a recipient, and provide plain-text and HTML versions of the same body. Save the message as text-and-html.eml.

Chilkat C Downloads

C
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;

    success = FALSE;

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

    email = CkEmail_Create();
    CkEmail_putFromName(email,"Sam Lee");
    CkEmail_putFromAddress(email,"sam@example.com");
    CkEmail_putReplyTo(email,"replies@example.com");
    success = CkEmail_AddTo(email,"Alex Rivera","alex@example.net");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    CkEmail_putSubject(email,"Sample message");
    CkEmail_putCharset(email,"utf-8");
    //  A custom header can carry a reference alongside the standard message fields.
    CkEmail_AddHeaderField(email,"X-Reference-ID","REF-42");
    success = CkEmail_AddPlainTextAlternativeBody(email,"Read more at https://example.com/details.");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    success = CkEmail_AddHtmlAlternativeBody(email,"<p><a href='https://example.com/details'>Read more</a></p>");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    success = CkEmail_SaveEml(email,"text-and-html.eml");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    printf("Saved text-and-html.eml.\n");


    CkEmail_Dispose(email);

    }