Unicode C
Unicode C
Save All MIME Attachments to Files
See more MIME Examples
Demonstrates the Chilkat Mime.PartsToFiles method, which recursively traverses the MIME tree and saves each part having a nonempty filename parameter to a directory. The first argument is the destination directory and the second is a StringTable that receives the saved filenames.
Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.
Background: This extracts every named attachment from a message in one call, walking the whole tree so it finds attachments nested inside multipart containers. It saves only parts that declare a
filename — inline body parts without one are skipped — and reports what it wrote via the StringTable, which is handy for logging or follow-up processing. The destination directory must already exist.Chilkat Unicode C Downloads
#include <C_CkMimeW.h>
#include <C_CkStringTableW.h>
void ChilkatSample(void)
{
BOOL success;
HCkMimeW mime;
HCkStringTableW savedFiles;
int n;
int i;
const wchar_t *name;
success = FALSE;
mime = CkMimeW_Create();
success = CkMimeW_LoadMimeFile(mime,L"qa_data/multipart_message.eml");
if (success == FALSE) {
wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
CkMimeW_Dispose(mime);
return;
}
// Recursively traverse the MIME tree and save each part that has a nonempty "filename" parameter
// to a directory. The 1st argument is the destination directory and the 2nd is a StringTable
// that receives the saved filenames.
savedFiles = CkStringTableW_Create();
success = CkMimeW_PartsToFiles(mime,L"qa_output/attachments",savedFiles);
if (success == FALSE) {
wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
CkMimeW_Dispose(mime);
CkStringTableW_Dispose(savedFiles);
return;
}
n = CkStringTableW_getCount(savedFiles);
wprintf(L"Saved %d files:\n",n);
for (i = 0; i <= n - 1; i++) {
name = CkStringTableW_stringAt(savedFiles,i);
if (CkStringTableW_getLastMethodSuccess(savedFiles) == FALSE) {
wprintf(L"%s\n",CkStringTableW_lastErrorText(savedFiles));
CkMimeW_Dispose(mime);
CkStringTableW_Dispose(savedFiles);
return;
}
wprintf(L"%s\n",name);
}
CkMimeW_Dispose(mime);
CkStringTableW_Dispose(savedFiles);
}