Sample code for 30+ languages & platforms
Objective-C

Fetch a Single Message's MIME into a StringBuilder

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.

Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw 8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.

Chilkat Objective-C Downloads

Objective-C
#import <CkoImap.h>
#import <CkoMessageSet.h>
#import <CkoStringBuilder.h>

BOOL success = NO;

//  Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
//  StringBuilder.  The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
//  number, and the 3rd is the StringBuilder (cleared first).

CkoImap *imap = [[CkoImap alloc] init];

imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];

success = [imap Connect: @"imap.example.com"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

success = [imap Login: @"user@example.com" password: @"myPassword"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

success = [imap SelectMailbox: @"Inbox"];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

//  Search for the message ids to download, returning UIDs.
BOOL wantUids = YES;
CkoMessageSet *msgSet = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"ALL" bUid: wantUids msgSet: msgSet];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}

if ([msgSet.Count intValue] > 0) {
    unsigned long uid = [msgSet GetId: [NSNumber numberWithInt: 0]];

    //  Download the message's MIME into a StringBuilder.  The id is a UID.
    BOOL useUid = YES;
    CkoStringBuilder *sbMime = [[CkoStringBuilder alloc] init];
    success = [imap FetchSingleAsMimeSb: uid bUid: useUid sbMime: sbMime];
    if (success == NO) {
        NSLog(@"%@",imap.LastErrorText);
        return;
    }

    NSLog(@"%@%d%@",@"MIME length: ",[sbMime.Length intValue],@" chars");
}

success = [imap Disconnect];
if (success == NO) {
    NSLog(@"%@",imap.LastErrorText);
    return;
}