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

POP3 Implicit TLS: Gmail OAuth2 on Port 995

Authenticate to Gmail on port 995 with implicit TLS and inspect headers for up to ten currently available POP messages. UIDLs stay as strings. With headerOnly=true, a zero line-count argument retrieves headers plus the first body line. The example displays only headers and issues TOP, neither RETR nor DELE.

Enable POP in Gmail settings and review the option controlling Gmail's copy after access. Delivery windows, history, and batching mean available POP mail is not the entire Gmail mailbox. Use IMAP for folder/label access and synchronization.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

Implicit TLS starts when the TCP connection opens. It is different from STLS on port 110. The historical password-based Gmail sample has been replaced with OAuth2.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat Objective-C Downloads

Objective-C
#import <CkoJsonObject.h>
#import <NSString.h>
#import <CkoMailMan.h>
#import <CkoStringTable.h>
#import <CkoEmail.h>

BOOL success = NO;

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

CkoJsonObject *token = [[CkoJsonObject alloc] init];
success = [token LoadFile: @"gmail-mail-token.json"];
if (success != YES) {
    NSLog(@"%@",@"Could not read gmail-mail-token.json from the private working directory.");
    return;
}

if (([[token JsonTypeOf: @"access_token"] intValue] != 1) || ([token StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"An OAuth access token is required. Authorize or refresh the account first.");
    return;
}

NSString *mailbox = @"user@example.com";
CkoMailMan *mailman = [[CkoMailMan alloc] init];
mailman.MailHost = @"pop.gmail.com";
mailman.MailPort = [NSNumber numberWithInt:995];
mailman.PopSsl = YES;
mailman.RequireSslCertVerify = YES;
mailman.PopUsername = mailbox;
mailman.PopPassword = @"";
mailman.OAuth2AccessToken = [token StringOf: @"access_token"];
success = [mailman Pop3Connect];
if (success != YES) {
    NSLog(@"%@",@"Could not establish the Gmail POP3 TLS connection.");
    return;
}

success = [mailman Pop3Authenticate];
if (success != YES) {
    [mailman Pop3EndSessionNoQuit];
    NSLog(@"%@",@"POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.");
    return;
}

CkoStringTable *uidls = [[CkoStringTable alloc] init];
success = [mailman FetchUidls: uidls];
if (success != YES) {
    [mailman Pop3EndSessionNoQuit];
    NSLog(@"%@",@"Could not list available POP UIDLs.");
    return;
}

CkoEmail *email = [[CkoEmail alloc] init];
int i = 0;
while (((i < [uidls.Count intValue]) && (i < 10))) {
    NSString *uidl = [uidls StringAt: [NSNumber numberWithInt: i]];
    //  With headerOnly=true, zero requests headers plus the first body line.
    //  Chilkat uses TOP rather than RETR; this example does not issue DELE.
    success = [mailman FetchByUidl: uidl headerOnly: YES numBodyLines: [NSNumber numberWithInt: 0] email: email];
    if (success != YES) {
        [mailman Pop3EndSessionNoQuit];
        NSLog(@"%@",@"Could not read message headers.");
        return;
    }

    NSLog(@"%@%@",@"UIDL: ",uidl);
    NSLog(@"%@%@",@"From: ",email.From);
    NSLog(@"%@%@",@"Subject: ",email.Subject);
    i = i + 1;
}

success = [mailman Pop3EndSession];
if (success != YES) {
    NSLog(@"%@",@"Headers were read, but closing the POP session failed.");
    return;
}

NSLog(@"%@%d",@"Headers inspected: ",i);