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

SendGrid: Send a plain text email to one recipient

See more SendGrid Examples

Send a plain text confirmation with one recipient, one subject, and one body. To try the request without sending mail, set validateOnly to true: sandbox mode returns HTTP 200 for a valid request. A normal send returns HTTP 202 when accepted; subsequent delivery is a separate event.

Use an API key with Mail Send permission and a verified sender identity. Replace the example addresses before sending.

SendGrid documentation: Sandbox mode.

Chilkat C Downloads

C
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    HCkJsonObject message;
    BOOL validateOnly;
    int expectedStatus;
    HCkHttpResponse response;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    http = CkHttp_Create();
    CkHttp_putRequireSslCertVerify(http,TRUE);
    //  Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
    CkHttp_putAuthToken(http,"SENDGRID_API_KEY");

    message = CkJsonObject_Create();
    //  Replace the sender with a verified sender identity and the recipient with your destination.
    CkJsonObject_UpdateString(message,"from.email","sender@example.com");
    CkJsonObject_UpdateString(message,"personalizations[0].to[0].email","recipient@example.com");
    CkJsonObject_UpdateString(message,"subject","Order confirmation");
    CkJsonObject_UpdateString(message,"content[0].type","text/plain");
    CkJsonObject_UpdateString(message,"content[0].value","We received your order #1042.");

    //  Set to true to validate the request without delivering an email.
    validateOnly = FALSE;
    CkJsonObject_UpdateBool(message,"mail_settings.sandbox_mode.enable",validateOnly);
    expectedStatus = 202;
    if (validateOnly == TRUE) {
        expectedStatus = 200;
    }

    response = CkHttpResponse_Create();
    CkHttp_HttpJson(http,"POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",response);
    //  HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
    if (CkHttpResponse_getStatusCode(response) == 0) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(message);
        CkHttpResponse_Dispose(response);
        return;
    }

    if (CkHttpResponse_getStatusCode(response) != expectedStatus) {
        printf("Mail Send returned HTTP %d\n",CkHttpResponse_getStatusCode(response));
        //  For validation errors the response body identifies the offending field and explains why.
        printf("%s\n",CkHttpResponse_bodyStr(response));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(message);
        CkHttpResponse_Dispose(response);
        return;
    }

    if (validateOnly == TRUE) {
        printf("Sandbox validation passed. No email was sent.\n");
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(message);
        CkHttpResponse_Dispose(response);
        return;
    }

    printf("SendGrid accepted the email for delivery (HTTP 202).\n");


    CkHttp_Dispose(http);
    CkJsonObject_Dispose(message);
    CkHttpResponse_Dispose(response);

    }