Objective-C
Objective-C
Imap.GetMailSize vs Email.Size
Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.Chilkat Objective-C Downloads
#import <CkoImap.h>
#import <CkoMessageSet.h>
#import <CkoEmailBundle.h>
#import <CkoEmail.h>
BOOL success = NO;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
CkoImap *imap = [[CkoImap alloc] init];
// Connect to an IMAP server.
// Use TLS
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
success = [imap Connect: @"imap.example.com"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Login
success = [imap Login: @"****" password: @"****"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Select an IMAP mailbox
success = [imap SelectMailbox: @"Inbox"];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
BOOL fetchUids = YES;
CkoMessageSet *messageSet = [[CkoMessageSet alloc] init];
success = [imap QueryMbx: @"ALL" bUid: fetchUids msgSet: messageSet];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
CkoEmailBundle *bundle = [[CkoEmailBundle alloc] init];
BOOL headersOnly = YES;
success = [imap FetchMsgSet: headersOnly msgSet: messageSet bundle: bundle];
if (success == NO) {
NSLog(@"%@",imap.LastErrorText);
return;
}
// Loop over the email objects and display information about each:
CkoEmail *email = [[CkoEmail alloc] init];
int i = 0;
int j = 0;
int numEmails = [bundle.MessageCount intValue];
while (i < numEmails) {
[bundle EmailAt: [NSNumber numberWithInt: i] email: email];
// Display the From and Subject
NSLog(@"%@",email.From);
NSLog(@"%@",email.Subject);
// Display the recipients:
j = 0;
while (j < [email.NumTo intValue]) {
NSLog(@"%@%@%@%@",@"TO: ",[email GetToName: [NSNumber numberWithInt: j]],@", ",[email GetToAddr: [NSNumber numberWithInt: j]]);
j = j + 1;
}
j = 0;
while (j < [email.NumCC intValue]) {
NSLog(@"%@%@%@%@",@"CC: ",[email GetCcName: [NSNumber numberWithInt: j]],@", ",[email GetCcAddr: [NSNumber numberWithInt: j]]);
j = j + 1;
}
// Show the total size of the email, including body and attachments:
NSLog(@"%d",[email.Size intValue]);
// When a full email is downloaded, we would use the
// email.NumAttachments property in conjunction with the
// email.GetMailAttach* methods.
// However, when an email object contains only the header,
// we need to access the attachment info differently:
int numAttach = [[imap GetMailNumAttach: email] intValue];
j = 0;
while (j < numAttach) {
NSLog(@"%@",[imap GetMailAttachFilename: email attachIndex: [NSNumber numberWithInt: j]]);
int attachSize = [[imap GetMailAttachSize: email attachIndex: [NSNumber numberWithInt: j]] intValue];
NSLog(@"%@%d%@",@" size = ",attachSize,@" bytes");
j = j + 1;
}
NSLog(@"%@",@"--");
i = i + 1;
}
// Disconnect from the IMAP server.
success = [imap Disconnect];