Sample code for 30+ languages & platforms
Delphi ActiveX Requires Chilkat v11.0.0+

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.

Reference documentation

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
token: TChilkatJsonObject;
mailbox: WideString;
mailman: TChilkatMailMan;
uidls: TChilkatStringTable;
email: TChilkatEmail;
i: Integer;
uidl: WideString;

begin
success := 0;

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

token := TChilkatJsonObject.Create(Self);
success := token.LoadFile('gmail-mail-token.json');
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not read gmail-mail-token.json from the private working directory.');
    Exit;
  end;
if ((token.JsonTypeOf('access_token') <> 1) or (token.StringOfEquals('access_token','',1) = 1)) then
  begin
    Memo1.Lines.Add('An OAuth access token is required. Authorize or refresh the account first.');
    Exit;
  end;
mailbox := 'user@example.com';
mailman := TChilkatMailMan.Create(Self);
mailman.MailHost := 'pop.gmail.com';
mailman.MailPort := 995;
mailman.PopSsl := 1;
mailman.RequireSslCertVerify := 1;
mailman.PopUsername := mailbox;
mailman.PopPassword := '';
mailman.OAuth2AccessToken := token.StringOf('access_token');
success := mailman.Pop3Connect();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Could not establish the Gmail POP3 TLS connection.');
    Exit;
  end;
success := mailman.Pop3Authenticate();
if (success <> 1) then
  begin
    mailman.Pop3EndSessionNoQuit();
    Memo1.Lines.Add('POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy.');
    Exit;
  end;
uidls := TChilkatStringTable.Create(Self);
success := mailman.FetchUidls(uidls.ControlInterface);
if (success <> 1) then
  begin
    mailman.Pop3EndSessionNoQuit();
    Memo1.Lines.Add('Could not list available POP UIDLs.');
    Exit;
  end;
email := TChilkatEmail.Create(Self);
i := 0;
while ((i < uidls.Count) and (i < 10)) do
  begin
    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,1,0,email.ControlInterface);
    if (success <> 1) then
      begin
        mailman.Pop3EndSessionNoQuit();
        Memo1.Lines.Add('Could not read message headers.');
        Exit;
      end;
    Memo1.Lines.Add('UIDL: ' + uidl);
    Memo1.Lines.Add('From: ' + email.From);
    Memo1.Lines.Add('Subject: ' + email.Subject);
    i := i + 1;
  end;

success := mailman.Pop3EndSession();
if (success <> 1) then
  begin
    Memo1.Lines.Add('Headers were read, but closing the POP session failed.');
    Exit;
  end;
Memo1.Lines.Add('Headers inspected: ' + IntToStr(i));