Unicode C Requires Chilkat v11.0.0+
Unicode C
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 Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
HCkHttpW http;
HCkJsonObjectW message;
BOOL validateOnly;
int expectedStatus;
HCkHttpResponseW response;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
// Supply a Mail Send API key from your application configuration. AuthToken adds the Bearer prefix.
CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
message = CkJsonObjectW_Create();
// Replace the sender with a verified sender identity and the recipient with your destination.
CkJsonObjectW_UpdateString(message,L"from.email",L"sender@example.com");
CkJsonObjectW_UpdateString(message,L"personalizations[0].to[0].email",L"recipient@example.com");
CkJsonObjectW_UpdateString(message,L"subject",L"Order confirmation");
CkJsonObjectW_UpdateString(message,L"content[0].type",L"text/plain");
CkJsonObjectW_UpdateString(message,L"content[0].value",L"We received your order #1042.");
// Set to true to validate the request without delivering an email.
validateOnly = FALSE;
CkJsonObjectW_UpdateBool(message,L"mail_settings.sandbox_mode.enable",validateOnly);
expectedStatus = 202;
if (validateOnly == TRUE) {
expectedStatus = 200;
}
response = CkHttpResponseW_Create();
CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",response);
// HttpJson also returns false for HTTP errors. A zero status means no HTTP response arrived.
if (CkHttpResponseW_getStatusCode(response) == 0) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(message);
CkHttpResponseW_Dispose(response);
return;
}
if (CkHttpResponseW_getStatusCode(response) != expectedStatus) {
wprintf(L"Mail Send returned HTTP %d\n",CkHttpResponseW_getStatusCode(response));
// For validation errors the response body identifies the offending field and explains why.
wprintf(L"%s\n",CkHttpResponseW_bodyStr(response));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(message);
CkHttpResponseW_Dispose(response);
return;
}
if (validateOnly == TRUE) {
wprintf(L"Sandbox validation passed. No email was sent.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(message);
CkHttpResponseW_Dispose(response);
return;
}
wprintf(L"SendGrid accepted the email for delivery (HTTP 202).\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(message);
CkHttpResponseW_Dispose(response);
}