Sample code for 30+ languages & platforms
Ruby 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 Ruby Downloads

Ruby
require 'chilkat'

success = false

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

token = Chilkat::CkJsonObject.new()
success = token.LoadFile("gmail-mail-token.json")
if (success != true)
    print "Could not read gmail-mail-token.json from the private working directory." + "\n";
    exit
end

if ((token.JsonTypeOf("access_token") != 1) or (token.StringOfEquals("access_token","",true) == true))
    print "An OAuth access token is required. Authorize or refresh the account first." + "\n";
    exit
end

mailbox = "user@example.com"
mailman = Chilkat::CkMailMan.new()
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)
    print "Could not establish the Gmail POP3 TLS connection." + "\n";
    exit
end

success = mailman.Pop3Authenticate()
if (success != true)
    mailman.Pop3EndSessionNoQuit()
    print "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy." + "\n";
    exit
end

uidls = Chilkat::CkStringTable.new()
success = mailman.FetchUidls(uidls)
if (success != true)
    mailman.Pop3EndSessionNoQuit()
    print "Could not list available POP UIDLs." + "\n";
    exit
end

email = Chilkat::CkEmail.new()
i = 0
while ((i < uidls.get_Count()) and (i < 10))
    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()
        print "Could not read message headers." + "\n";
        exit
    end

    print "UIDL: " + uidl + "\n";
    print "From: " + email.ck_from() + "\n";
    print "Subject: " + email.subject() + "\n";
    i = i + 1
end
success = mailman.Pop3EndSession()
if (success != true)
    print "Headers were read, but closing the POP session failed." + "\n";
    exit
end

print "Headers inspected: " + i.to_s() + "\n";