Objective-C
Objective-C
Reading Unread POP3 Email
The POP3 protocol cannot determine which emails are "unread," and pure POP3 servers do not store this information. Servers like Exchange Server, offering both POP3 and IMAP interfaces, do contain read/unread data, but it's only accessible through IMAP. Email clients like Outlook and Thunderbird store read/unread statuses on the client side. The example demonstrates using UIDLs to track and manage "unread" emails.Chilkat Objective-C Downloads
#import <CkoMailMan.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
#import <CkoHashtable.h>
#import <CkoFileAccess.h>
#import <CkoStringTable.h>
#import <CkoEmail.h>
BOOL success = NO;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// The mailman object is used for receiving (POP3)
// and sending (SMTP) email.
CkoMailMan *mailman = [[CkoMailMan alloc] init];
// Set the POP3 server's hostname
mailman.MailHost = @"pop.example.com";
// Set the POP3 login/password.
mailman.PopUsername = @"***";
mailman.PopPassword = @"***";
// Keep a records of already-seen UIDLs in hash table serialized to an XML file.
NSString *seenUidlsPath = @"c:/temp/seenUidls.xml";
CkoStringBuilder *sbXml = [[CkoStringBuilder alloc] init];
CkoHashtable *htSeenUidls = [[CkoHashtable alloc] init];
CkoFileAccess *fac = [[CkoFileAccess alloc] init];
if ([fac FileExists: seenUidlsPath] == YES) {
success = [sbXml LoadFile: seenUidlsPath charset: @"utf-8"];
if (success == NO) {
NSLog(@"%@",sbXml.LastErrorText);
return;
}
[htSeenUidls AddFromXmlSb: sbXml];
}
// Get the complete list of UIDLs on the mail server.
CkoStringTable *stUidls = [[CkoStringTable alloc] init];
success = [mailman FetchUidls: stUidls];
if (success == NO) {
NSLog(@"%@",mailman.LastErrorText);
return;
}
// Build a list of unseen UIDLs
CkoStringTable *stUnseenUidls = [[CkoStringTable alloc] init];
NSString *uidl = 0;
int i = 0;
int count = [stUidls.Count intValue];
while (i < count) {
uidl = [stUidls StringAt: [NSNumber numberWithInt: i]];
if ([htSeenUidls Contains: uidl] != YES) {
[stUnseenUidls Append: uidl];
}
i = i + 1;
}
if ([stUnseenUidls.Count intValue] == 0) {
NSLog(@"%@",@"No unseen emails!");
return;
}
// Download the unseen emails, adding each UIDL to the "seen" hash table.
CkoEmail *email = [[CkoEmail alloc] init];
count = [stUnseenUidls.Count intValue];
i = 0;
while (i < count) {
// Download the full email.
uidl = [stUnseenUidls StringAt: [NSNumber numberWithInt: i]];
success = [mailman FetchByUidl: uidl headerOnly: NO numBodyLines: [NSNumber numberWithInt: 0] email: email];
if (success == NO) {
NSLog(@"%@",mailman.LastErrorText);
return;
}
NSLog(@"%d",i);
NSLog(@"%@%@",@"From: ",email.From);
NSLog(@"%@%@",@"Subject: ",email.Subject);
// Add this UIDL to the "seen" hash table.
[htSeenUidls AddStr: uidl value: @""];
i = i + 1;
}
[mailman Pop3EndSession];
// Update the "seen" UIDLs file.
[sbXml Clear];
[htSeenUidls ToXmlSb: sbXml];
success = [sbXml WriteFile: seenUidlsPath charset: @"utf-8" emitBom: NO];
if (success == NO) {
NSLog(@"%@",sbXml.LastErrorText);
return;
}
NSLog(@"%@",@"Success.");