Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) SendGrid HTML Email with Embedded ImagesSee more SendGrid ExamplesDemonstrates how to send an HTML email with embedded images using SendGrid.
#include <C_CkHttpRequestW.h> #include <C_CkHttpW.h> #include <C_CkBinDataW.h> #include <C_CkStringBuilderW.h> #include <C_CkJsonObjectW.h> #include <C_CkHttpResponseW.h> void ChilkatSample(void) { HCkHttpRequestW req; HCkHttpW http; BOOL success; HCkBinDataW bdJpg; HCkStringBuilderW sbHtml; HCkJsonObjectW json; HCkHttpResponseW resp; // This requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code req = CkHttpRequestW_Create(); http = CkHttpW_Create(); // First.. load a JPG file and build an HTML img tag with the JPG data inline encoded. // Our HTML img tag will look like this: // <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEA3ADcAA..." alt="" style="border-width:0px;" /> bdJpg = CkBinDataW_Create(); success = CkBinDataW_LoadFile(bdJpg,L"qa_data/jpg/starfish.jpg"); if (success == FALSE) { wprintf(L"Failed to load JPG file.\n"); CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); CkBinDataW_Dispose(bdJpg); return; } // Note: HTML containing embedded base64 image data may not be visible in all email clients. // For example, GMail might not display the image, but viewing in Outlook might be OK. sbHtml = CkStringBuilderW_Create(); CkStringBuilderW_Append(sbHtml,L"<html><body><b>This is a test<b><br /><img src=\"data:image/jpeg;base64,"); // Append the base64 image data to sbHtml. CkBinDataW_GetEncodedSb(bdJpg,L"base64",sbHtml); CkStringBuilderW_Append(sbHtml,L"\" alt=\"\" style=\"border-width:0px;\" /></body></html>"); json = CkJsonObjectW_Create(); CkJsonObjectW_UpdateString(json,L"personalizations[0].to[0].email",L"matt@chilkat.io"); CkJsonObjectW_UpdateString(json,L"personalizations[0].to[0].name",L"Matt"); CkJsonObjectW_UpdateString(json,L"from.email",L"admin@chilkatsoft.com"); CkJsonObjectW_UpdateString(json,L"subject",L"Test HTML email with images"); CkJsonObjectW_UpdateString(json,L"content[0].type",L"text/html"); CkJsonObjectW_UpdateSb(json,L"content[0].value",sbHtml); CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY"); resp = CkHttpW_PostJson3(http,L"https://api.sendgrid.com/v3/mail/send",L"application/json",json); if (CkHttpW_getLastMethodSuccess(http) != TRUE) { wprintf(L"%s\n",CkHttpW_lastErrorText(http)); } else { wprintf(L"response status code: %d\n",CkHttpResponseW_getStatusCode(resp)); // Display the response. // If successful, the response code is 202 and the response body string is empty. // (The response body string may also be empty for error response codes.) wprintf(L"%s\n",CkHttpResponseW_bodyStr(resp)); CkHttpResponseW_Dispose(resp); } CkHttpRequestW_Dispose(req); CkHttpW_Dispose(http); CkBinDataW_Dispose(bdJpg); CkStringBuilderW_Dispose(sbHtml); CkJsonObjectW_Dispose(json); } |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.