C
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 C Downloads
#include <C_CkEmail.h>
#include <C_CkBinData.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkEmail email;
HCkBinData bdImage;
const char *cid;
HCkStringBuilder 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 = CkEmail_Create();
CkEmail_putSubject(email,"Related image from BinData");
// Load the image data from a file into a BinData object.
bdImage = CkBinData_Create();
success = CkBinData_LoadFile(bdImage,"qa_data/images/logo.png");
if (success == FALSE) {
printf("%s\n",CkBinData_lastErrorText(bdImage));
CkEmail_Dispose(email);
CkBinData_Dispose(bdImage);
return;
}
// Add the image as a related item; capture its generated Content-ID.
cid = CkEmail_addRelatedBd(email,"logo.png",bdImage);
if (CkEmail_getLastMethodSuccess(email) == FALSE) {
printf("%s\n",CkEmail_lastErrorText(email));
CkEmail_Dispose(email);
CkBinData_Dispose(bdImage);
return;
}
// Reference the related item in the HTML body by its Content-ID.
sbHtml = CkStringBuilder_Create();
CkStringBuilder_Append(sbHtml,"<html><body><img src=\"cid:PLACEHOLDER_CID\"/></body></html>");
numReplaced = CkStringBuilder_Replace(sbHtml,"PLACEHOLDER_CID",cid);
CkEmail_SetHtmlBody(email,CkStringBuilder_getAsString(sbHtml));
printf("NumRelatedItems = %d\n",CkEmail_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.
CkEmail_Dispose(email);
CkBinData_Dispose(bdImage);
CkStringBuilder_Dispose(sbHtml);
}