Unicode C
Unicode C
Add a Related Item from a BinData Object
See more Email Object Examples
Demonstrates the Chilkat Email.AddRelatedBd method, which adds a related item (such as an inline image) using the contents of a BinData object, and returns the generated Content-ID. This example loads an image into a BinData, adds it, captures the Content-ID, and references it from the HTML body.
Background: This is the binary,
Content-ID-based way to embed an inline resource — the right choice for images, whose bytes belong in a BinData rather than a string. Because Chilkat generates the Content-ID, the usual pattern is: add the item first, capture the returned ID, then build the matching <img src="cid:..."> reference from it (done here with a StringBuilder).Chilkat Unicode C Downloads
#include <C_CkEmailW.h>
#include <C_CkBinDataW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkEmailW email;
HCkBinDataW bdImage;
const wchar_t *cid;
HCkStringBuilderW sbHtml;
int numReplaced;
success = FALSE;
// Demonstrates the AddRelatedBd method, which adds a related item (such as an inline image)
// using the contents of a BinData object, and returns the generated Content-ID.
email = CkEmailW_Create();
CkEmailW_putSubject(email,L"Related image from BinData");
// Load the image data from a file into a BinData object.
bdImage = CkBinDataW_Create();
success = CkBinDataW_LoadFile(bdImage,L"qa_data/images/logo.png");
if (success == FALSE) {
wprintf(L"%s\n",CkBinDataW_lastErrorText(bdImage));
CkEmailW_Dispose(email);
CkBinDataW_Dispose(bdImage);
return true;
}
// Add the image as a related item; capture its generated Content-ID.
cid = CkEmailW_addRelatedBd(email,L"logo.png",bdImage);
if (CkEmailW_getLastMethodSuccess(email) == FALSE) {
wprintf(L"%s\n",CkEmailW_lastErrorText(email));
CkEmailW_Dispose(email);
CkBinDataW_Dispose(bdImage);
return true;
}
// Reference the related item in the HTML body by its Content-ID.
sbHtml = CkStringBuilderW_Create();
CkStringBuilderW_Append(sbHtml,L"<html><body><img src=\"cid:PLACEHOLDER_CID\"/></body></html>");
numReplaced = CkStringBuilderW_Replace(sbHtml,L"PLACEHOLDER_CID",cid);
CkEmailW_SetHtmlBody(email,CkStringBuilderW_getAsString(sbHtml));
wprintf(L"NumRelatedItems = %d\n",CkEmailW_getNumRelatedItems(email));
// Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
// relative to the current working directory of the running application.
CkEmailW_Dispose(email);
CkBinDataW_Dispose(bdImage);
CkStringBuilderW_Dispose(sbHtml);
}