Gmail POP3: Read Message Headers with OAuth2
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.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat C++ Downloads
#include <CkJsonObject.h>
#include <CkMailMan.h>
#include <CkStringTable.h>
#include <CkEmail.h>
void ChilkatSample(void)
{
bool success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkJsonObject token;
success = token.LoadFile("gmail-mail-token.json");
if (success != true) {
std::cout << "Could not read gmail-mail-token.json from the private working directory." << "\r\n";
return;
}
if ((token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true)) {
std::cout << "An OAuth access token is required. Authorize or refresh the account first." << "\r\n";
return;
}
const char *mailbox = "user@example.com";
CkMailMan mailman;
mailman.put_MailHost("pop.gmail.com");
mailman.put_MailPort(995);
mailman.put_PopSsl(true);
mailman.put_RequireSslCertVerify(true);
mailman.put_PopUsername(mailbox);
mailman.put_PopPassword("");
mailman.put_OAuth2AccessToken(token.stringOf("access_token"));
success = mailman.Pop3Connect();
if (success != true) {
std::cout << "Could not establish the Gmail POP3 TLS connection." << "\r\n";
return;
}
success = mailman.Pop3Authenticate();
if (success != true) {
mailman.Pop3EndSessionNoQuit();
std::cout << "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy." << "\r\n";
return;
}
CkStringTable uidls;
success = mailman.FetchUidls(uidls);
if (success != true) {
mailman.Pop3EndSessionNoQuit();
std::cout << "Could not list available POP UIDLs." << "\r\n";
return;
}
CkEmail email;
int i = 0;
while (((i < uidls.get_Count()) && (i < 10))) {
const char *uidl = uidls.stringAt(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,true,0,email);
if (success != true) {
mailman.Pop3EndSessionNoQuit();
std::cout << "Could not read message headers." << "\r\n";
return;
}
std::cout << "UIDL: " << uidl << "\r\n";
std::cout << "From: " << email.ck_from() << "\r\n";
std::cout << "Subject: " << email.subject() << "\r\n";
i = i + 1;
}
success = mailman.Pop3EndSession();
if (success != true) {
std::cout << "Headers were read, but closing the POP session failed." << "\r\n";
return;
}
std::cout << "Headers inspected: " << i << "\r\n";
}