Gmail: Authenticate with an App Password
See more GMail SMTP/IMAP/POP Examples
Authenticate to Gmail IMAP, SMTP, or POP3 with a Google app password. Choose one protocol in the code; the example authenticates and disconnects without sending, retrieving, or deleting messages.
OAuth2 is the recommended approach. An app password is an account-specific 16-character password generated under Google Account security settings after enabling 2-Step Verification. Use the generated value without display spaces, not your normal Google password. App passwords may be unavailable under Advanced Protection, security-key-only 2-Step Verification, or organization policy. Changing the Google account password revokes its app passwords.
Store the app password in protected configuration and load it at runtime. It is a credential, not an OAuth token, so this example uses the protocol password property and does not set OAuth2AccessToken. There is no supported instruction to enable “less secure apps.” POP must be enabled in Gmail settings; Workspace administrators can also restrict IMAP and POP. Personal Gmail IMAP is always enabled.
Chilkat Objective-C Downloads
#import <NSString.h>
#import <CkoImap.h>
#import <CkoMailMan.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Choose "imap", "smtp", or "pop". This example only tests authentication.
NSString *protocol = @"imap";
NSString *mailbox = @"user@example.com";
// Replace this placeholder with a value loaded from protected configuration.
NSString *appPassword = @"YOUR_16_CHARACTER_APP_PASSWORD";
if (protocol == @"imap") {
CkoImap *imap = [[CkoImap alloc] init];
imap.Ssl = YES;
imap.Port = [NSNumber numberWithInt:993];
imap.RequireSslCertVerify = YES;
success = [imap Connect: @"imap.gmail.com"];
if (success != YES) {
NSLog(@"%@",@"Could not establish the Gmail IMAP TLS connection.");
return;
}
success = [imap Login: mailbox password: appPassword];
if (success != YES) {
[imap Disconnect];
NSLog(@"%@",@"IMAP authentication failed. Check the app password and account policy.");
return;
}
[imap Logout];
[imap Disconnect];
}
else {
CkoMailMan *mailman = [[CkoMailMan alloc] init];
mailman.RequireSslCertVerify = YES;
if (protocol == @"smtp") {
mailman.SmtpHost = @"smtp.gmail.com";
mailman.SmtpPort = [NSNumber numberWithInt:465];
mailman.SmtpSsl = YES;
mailman.StartTLS = NO;
mailman.SmtpUsername = mailbox;
mailman.SmtpPassword = appPassword;
success = [mailman SmtpConnect];
if (success != YES) {
NSLog(@"%@",@"Could not establish the Gmail SMTP TLS connection.");
return;
}
success = [mailman SmtpAuthenticate];
[mailman CloseSmtpConnection];
if (success != YES) {
NSLog(@"%@",@"SMTP authentication failed. Check the app password and account policy.");
return;
}
}
else {
if (protocol != @"pop") {
NSLog(@"%@",@"Choose imap, smtp, or pop.");
return;
}
mailman.MailHost = @"pop.gmail.com";
mailman.MailPort = [NSNumber numberWithInt:995];
mailman.PopSsl = YES;
mailman.PopUsername = mailbox;
mailman.PopPassword = appPassword;
success = [mailman Pop3Connect];
if (success != YES) {
NSLog(@"%@",@"Could not establish the Gmail POP3 TLS connection.");
return;
}
success = [mailman Pop3Authenticate];
// Disconnect without QUIT; this authentication test makes no mailbox changes.
[mailman Pop3EndSessionNoQuit];
if (success != YES) {
NSLog(@"%@",@"POP3 authentication failed. Check the app password, POP setting, and account policy.");
return;
}
}
}
NSLog(@"%@%@",@"Gmail authentication succeeded using ",protocol);