Sample code for 30+ languages & platforms
Unicode C

Set the Charset of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentCharset method, which sets the charset parameter of the Content-Type header field for the attachment at a given zero-based index. This example adds a text attachment and sets its charset to utf-8.

Background: For a text attachment, the charset parameter tells the receiving client which character encoding the bytes use, so accented or non-Latin text renders correctly. Setting it explicitly (typically utf-8) removes ambiguity when the attachment contains non-ASCII content — without it, a client may guess wrong and display garbled characters.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>

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

    success = FALSE;

    //  Demonstrates the SetAttachmentCharset method, which sets the charset parameter of the
    //  Content-Type header field for the attachment at the given zero-based index.

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Set attachment charset");

    CkEmailW_AddStringAttachment(email,L"notes.txt",L"Some notes.");

    //  Set the charset of the first attachment (index 0) to utf-8.
    success = CkEmailW_SetAttachmentCharset(email,0,L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return true;
    }

    //  The attachment's Content-Type now includes charset="utf-8".
    wprintf(L"%s\n",CkEmailW_getMime(email));


    CkEmailW_Dispose(email);

    }