Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Find and save an attachment and list inline images
See more Email Object Examples
Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.
Chilkat Unicode C++ Downloads
#include <CkEmailW.h>
#include <CkStringBuilderW.h>
#include <CkBinDataW.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkEmailW email;
success = email.LoadEml(L"with-attachments.eml");
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
CkStringBuilderW filename;
CkStringBuilderW mediaType;
bool found = false;
int i = 0;
while ((i < email.get_NumAttachments())) {
filename.SetString(email.getAttachmentFilename(i));
mediaType.SetString(email.getAttachmentContentType(i));
if ((filename.ContentsEqual(L"agenda.csv",false) == true) && (mediaType.ContentsEqual(L"text/csv",false) == true) && (found == false)) {
CkBinDataW data;
success = email.GetAttachmentBd(i,data);
if (success != true) {
wprintf(L"%s\n",email.lastErrorText());
return;
}
success = data.WriteFile(L"received-agenda.csv");
if (success != true) {
wprintf(L"Failed: data.WriteFile.\n");
return;
}
wprintf(L"Saved attachment: %d bytes.\n",data.get_NumBytes());
found = true;
}
i = i + 1;
}
if (found != true) {
wprintf(L"No matching attachment.\n");
}
i = 0;
while ((i < email.get_NumRelatedItems())) {
wprintf(L"Related: %s Content-ID: %s\n",email.getRelatedContentType(i),email.getRelatedContentID(i));
i = i + 1;
}
}