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

SendGrid: Send product updates with a topic unsubscribe link

See more SendGrid Examples

Use the numeric ID of your existing Product updates unsubscribe group and a verified sender. The ASM tag expands to the recipient-specific link for this group. A click opts the recipient out of this topic; a global unsubscribe applies across topics.

Unsubscribe tags ยท Mail Send

Chilkat Unicode C Downloads

Unicode C
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkHttpW http;
    HCkHttpResponseW resp;
    HCkJsonObjectW message;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    success = FALSE;
    http = CkHttpW_Create();
    CkHttpW_putRequireSslCertVerify(http,TRUE);
    //  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
    CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
    resp = CkHttpResponseW_Create();

    message = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(message,L"from.email",L"sender@example.com");
    CkJsonObjectW_UpdateString(message,L"personalizations[0].to[0].email",L"recipient@example.com");
    CkJsonObjectW_UpdateInt(message,L"asm.group_id",12345);
    CkJsonObjectW_UpdateString(message,L"subject",L"Product updates: November edition");
    CkJsonObjectW_UpdateString(message,L"content[0].type",L"text/html");
    CkJsonObjectW_UpdateString(message,L"content[0].value",L"<p>The new catalog is available.</p><p><a href=\"<%asm_group_unsubscribe_raw_url%>\">Unsubscribe from product updates</a></p>");

    success = CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",resp);
    if (success != TRUE) {
        if (CkHttpResponseW_getStatusCode(resp) == 0) {
            wprintf(L"%s\n",CkHttpW_lastErrorText(http));
        }
        else {
            wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
        }

        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(message);
        return;
    }

    if (CkHttpResponseW_getStatusCode(resp) != 202) {
        wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
        CkHttpW_Dispose(http);
        CkHttpResponseW_Dispose(resp);
        CkJsonObjectW_Dispose(message);
        return;
    }

    wprintf(L"Accepted for delivery (HTTP 202).\n");


    CkHttpW_Dispose(http);
    CkHttpResponseW_Dispose(resp);
    CkJsonObjectW_Dispose(message);

    }