Objective-C
Objective-C
Get IMAP Attachment Sizes
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailAttachSize method, which returns the size in bytes of an attachment. The first argument is the Email and the second is the zero-based attachment index. This example fetches headers only and prints each attachment's name and size.
Background: Like the attachment filename, the size comes from
ckx-imap-* metadata and is available from a header-only fetch without downloading the body. A client can therefore show attachment sizes in a message preview, and decide whether to auto-download or prompt before pulling a large file.Chilkat Objective-C Downloads
#import <CkoImap.h>
#import <CkoEmail.h>
#import <NSString.h>
BOOL success = NO;
// Demonstrates the Imap.GetMailAttachSize method, which returns the size in bytes of an
// attachment. The 1st argument is the Email and the 2nd is the zero-based attachment index.
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;
}
// Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
// metadata describing the attachments is present, so the attachment info methods still work.
BOOL headersOnly = YES;
BOOL useUid = NO;
int seqNum = 1;
CkoEmail *email = [[CkoEmail alloc] init];
success = [imap FetchEmail: headersOnly msgId: seqNum bUid: useUid email: email];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
int numAttach = [[imap GetMailNumAttach: email] intValue];
int i;
for (i = 0; i <= numAttach - 1; i++) {
NSString *fname = [imap GetMailAttachFilename: email attachIndex: [NSNumber numberWithInt: i]];
if (imap.LastMethodSuccess == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
int sz = [[imap GetMailAttachSize: email attachIndex: [NSNumber numberWithInt: i]] intValue];
NSLog(@"%@%@%d%@",fname,@": ",sz,@" bytes");
}
success = [imap Disconnect];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}