Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

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 Objective-C Downloads

Objective-C
#import <CkoEmail.h>
#import <CkoStringBuilder.h>
#import <CkoBinData.h>

BOOL success = NO;

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

CkoEmail *email = [[CkoEmail alloc] init];
success = [email LoadEml: @"with-attachments.eml"];
if (success != YES) {
    NSLog(@"%@",email.LastErrorText);
    return;
}

CkoStringBuilder *filename = [[CkoStringBuilder alloc] init];
CkoStringBuilder *mediaType = [[CkoStringBuilder alloc] init];
BOOL found = NO;
int i = 0;
while ((i < [email.NumAttachments intValue])) {
    [filename SetString: [email GetAttachmentFilename: [NSNumber numberWithInt: i]]];
    [mediaType SetString: [email GetAttachmentContentType: [NSNumber numberWithInt: i]]];
    if (([filename ContentsEqual: @"agenda.csv" caseSensitive: NO] == YES) && ([mediaType ContentsEqual: @"text/csv" caseSensitive: NO] == YES) && (found == NO)) {
        CkoBinData *data = [[CkoBinData alloc] init];
        success = [email GetAttachmentBd: [NSNumber numberWithInt: i] binData: data];
        if (success != YES) {
            NSLog(@"%@",email.LastErrorText);
            return;
        }

        success = [data WriteFile: @"received-agenda.csv"];
        if (success != YES) {
            NSLog(@"%@",@"Failed: data.WriteFile.");
            return;
        }

        NSLog(@"%@%d%@",@"Saved attachment: ",[data.NumBytes intValue],@" bytes.");
        found = YES;
    }

    i = i + 1;
}

if (found != YES) {
    NSLog(@"%@",@"No matching attachment.");
}

i = 0;
while ((i < [email.NumRelatedItems intValue])) {
    NSLog(@"%@%@%@%@",@"Related: ",[email GetRelatedContentType: [NSNumber numberWithInt: i]],@"  Content-ID: ",[email GetRelatedContentID: [NSNumber numberWithInt: i]]);
    i = i + 1;
}