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

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token [new_CkJsonObject]

set success [CkJsonObject_LoadFile $token "gmail-mail-token.json"]
if {$success != 1} then {
    puts "Could not read gmail-mail-token.json from the private working directory."
    delete_CkJsonObject $token
    exit
}

if {expr [[CkJsonObject_JsonTypeOf $token "access_token"] != 1]  ||  [[CkJsonObject_StringOfEquals $token "access_token" "" 1] == 1]} then {
    puts "An OAuth access token is required. Authorize or refresh the account first."
    delete_CkJsonObject $token
    exit
}

set mailbox "user@example.com"
set mailman [new_CkMailMan]

CkMailMan_put_MailHost $mailman "pop.gmail.com"
CkMailMan_put_MailPort $mailman 995
CkMailMan_put_PopSsl $mailman 1
CkMailMan_put_RequireSslCertVerify $mailman 1
CkMailMan_put_PopUsername $mailman $mailbox
CkMailMan_put_PopPassword $mailman ""
CkMailMan_put_OAuth2AccessToken $mailman [CkJsonObject_stringOf $token "access_token"]
set success [CkMailMan_Pop3Connect $mailman]
if {$success != 1} then {
    puts "Could not establish the Gmail POP3 TLS connection."
    delete_CkJsonObject $token
    delete_CkMailMan $mailman
    exit
}

set success [CkMailMan_Pop3Authenticate $mailman]
if {$success != 1} then {
    CkMailMan_Pop3EndSessionNoQuit $mailman
    puts "POP3 authentication failed. Check the token, mailbox, POP setting, and Workspace policy."
    delete_CkJsonObject $token
    delete_CkMailMan $mailman
    exit
}

set uidls [new_CkStringTable]

set success [CkMailMan_FetchUidls $mailman $uidls]
if {$success != 1} then {
    CkMailMan_Pop3EndSessionNoQuit $mailman
    puts "Could not list available POP UIDLs."
    delete_CkJsonObject $token
    delete_CkMailMan $mailman
    delete_CkStringTable $uidls
    exit
}

set email [new_CkEmail]

set i 0
while {expr [$i < [CkStringTable_get_Count $uidls]]  &&  [$i < 10]} {
    set uidl [CkStringTable_stringAt $uidls $i]
    #  With headerOnly=true, zero requests headers plus the first body line.
    #  Chilkat uses TOP rather than RETR; this example does not issue DELE.
    set success [CkMailMan_FetchByUidl $mailman $uidl 1 0 $email]
    if {$success != 1} then {
        CkMailMan_Pop3EndSessionNoQuit $mailman
        puts "Could not read message headers."
        delete_CkJsonObject $token
        delete_CkMailMan $mailman
        delete_CkStringTable $uidls
        delete_CkEmail $email
        exit
    }

    puts "UIDL: $uidl"
    puts "From: [CkEmail_from $email]"
    puts "Subject: [CkEmail_subject $email]"
    set i [expr $i + 1]
}
set success [CkMailMan_Pop3EndSession $mailman]
if {$success != 1} then {
    puts "Headers were read, but closing the POP session failed."
    delete_CkJsonObject $token
    delete_CkMailMan $mailman
    delete_CkStringTable $uidls
    delete_CkEmail $email
    exit
}

puts "Headers inspected: $i"

delete_CkJsonObject $token
delete_CkMailMan $mailman
delete_CkStringTable $uidls
delete_CkEmail $email