C
C
Load .eml and Examine the Structure, Attachments, and Related Items
See more Email Object Examples
Demonstrates how to load examine the MIME structure of a .eml, and also examine the attachment and related item filenames, attached messages, and multipart/report and DSN information.Chilkat C Downloads
#include <C_CkMime.h>
#include <C_CkEmail.h>
#include <C_CkJsonObject.h>
void ChilkatSample(void)
{
BOOL success;
const char *emlPath;
HCkMime mime;
HCkEmail email;
int i;
int numAttach;
int numRelated;
HCkEmail em;
int numAttachedMessages;
int numReports;
HCkJsonObject jsonDsnInfo;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
emlPath = "C:/AAWorkarea/beatrix/roesner.eml";
mime = CkMime_Create();
success = CkMime_LoadMimeFile(mime,emlPath);
if (success == FALSE) {
printf("%s\n",CkMime_lastErrorText(mime));
CkMime_Dispose(mime);
return;
}
printf("---- MIME structure ----\n");
printf("%s\n",CkMime_getStructure(mime,"text"));
printf("------------------------\n");
email = CkEmail_Create();
success = CkEmail_LoadEml(email,emlPath);
// Was this a signed and/or encrypted email?
// If so, then loading the .eml automatically unwraps
// (i.e. verifies signatures and decrypts) and the resultant
// email is what existed prior to signing/encrypting.
printf("Email was Signed: %d\n",CkEmail_getReceivedSigned(email));
printf("Email was Encrypted: %d\n",CkEmail_getReceivedEncrypted(email));
if (CkEmail_getReceivedSigned(email) == TRUE) {
printf("Signature(s) valid = %d\n",CkEmail_getSignaturesValid(email));
}
if (CkEmail_getReceivedEncrypted(email) == TRUE) {
printf("Decrypted successfully = %d\n",CkEmail_getDecrypted(email));
}
i = 0;
numAttach = CkEmail_getNumAttachments(email);
printf("Number of attachments = %d\n",numAttach);
while (i < numAttach) {
printf("---- Attachment %d\n",i);
// Examine the filename (if any)
printf("filename: %s\n",CkEmail_getAttachmentFilename(email,i));
// Examine the content-ID (if any)
printf("Content-ID: %s\n",CkEmail_getAttachmentContentID(email,i));
// Examine the content-type
printf("Content-Type: %s\n",CkEmail_getAttachmentContentType(email,i));
// Examine the content-disposition
printf("Content-Disposition%s\n",CkEmail_getAttachmentHeader(email,i,"content-disposition"));
// Examine the attachment size:
printf("Size (in bytes) of the attachment: %d\n",CkEmail_GetAttachmentSize(email,i));
i = i + 1;
}
printf("--\n");
// Now for the related items.
// Note: A MIME sub-part can potentially be both a related item AND an attachment.
// The typical case is when the item is contained under the multipart/related enclosure and
// the item also has a "Content-Disposition" header indicating "attachment".
// The location within multipart/related makes it a "related item", yet the Content-Disposition can also make it semantically an attachment.
// Related items and attachments are not necessarily mutually exclusive.
numRelated = CkEmail_getNumRelatedItems(email);
printf("Number of related items = %d\n",numRelated);
i = 0;
while (i < numRelated) {
printf("---- Related Item %d\n",i);
// Examine the filename (if any)
printf("filename: %s\n",CkEmail_getRelatedFilename(email,i));
// Examine the content-ID (if any)
printf("Content-ID: %s\n",CkEmail_getRelatedContentID(email,i));
// Examine the content-type
printf("Content-Type: %s\n",CkEmail_getRelatedContentType(email,i));
// Examine the content-location (if any)
printf("Content-Location%s\n",CkEmail_getRelatedContentLocation(email,i));
i = i + 1;
}
// The email could also have attached messages.
// An attached message is another email that was attached to this email.
em = CkEmail_Create();
numAttachedMessages = CkEmail_getNumAttachedMessages(email);
printf("Number of attached messages = %d\n",numAttachedMessages);
i = 0;
while (i < numAttachedMessages) {
printf("---- Attached message %d\n",i);
// Examine the attached email
CkEmail_GetAttachedEmail(email,i,em);
printf("from: %s\n",CkEmail_ck_from(em));
printf("subject: %s\n",CkEmail_subject(em));
i = i + 1;
}
// An email could also be a multipart/report email.
// This is a DSN (Delivery Status Notification)
// The NumReports property indicates how many "reports" exist.
numReports = CkEmail_getNumReports(email);
printf("Number of reports = %d\n",numReports);
i = 0;
while (i < numReports) {
printf("---- Report %d\n",i);
// Get the raw report data...
printf("%s\n",CkEmail_getReport(email,i));
i = i + 1;
}
// If the email is a multipart/report, then the information
// from the message/delivery-status part of the email can be retrieved:
if (CkEmail_IsMultipartReport(email) == TRUE) {
printf("--- Delivery Status Information:\n");
printf("Status: %s\n",CkEmail_getDeliveryStatusInfo(email,"Status"));
printf("Action: %s\n",CkEmail_getDeliveryStatusInfo(email,"Action"));
printf("Reporting-MTA: %s\n",CkEmail_getDeliveryStatusInfo(email,"Reporting-MTA"));
jsonDsnInfo = CkJsonObject_Create();
CkEmail_GetDsnInfo(email,jsonDsnInfo);
CkJsonObject_putEmitCompact(jsonDsnInfo,FALSE);
printf("%s\n",CkJsonObject_emit(jsonDsnInfo));
}
CkMime_Dispose(mime);
CkEmail_Dispose(email);
CkEmail_Dispose(em);
CkJsonObject_Dispose(jsonDsnInfo);
}