IMAP: Capture a Session Log After Authentication
See more IMAP Examples
Use Gmail OAuth2 to demonstrate logging only a read-only EXAMINE exchange. Start logging after successful login, clear any earlier log, capture the result, then disable and clear logging before closing the session.
Authentication logs can contain bearer credentials, and later protocol traffic can contain private mailbox data. Keep diagnostic output private and redact it before sharing. This example deliberately excludes login and message retrieval from its captured log.
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
String sMailbox
Handle hoImap
String sDiagnostic
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-mail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-mail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "An OAuth access token is required. Authorize or refresh the account first."
Procedure_Return
End
Move "user@example.com" To sMailbox
Get Create (RefClass(cComChilkatImap)) To hoImap
If (Not(IsComObjectCreated(hoImap))) Begin
Send CreateComObject of hoImap
End
Set ComSsl Of hoImap To True
Set ComPort Of hoImap To 993
Set ComRequireSslCertVerify Of hoImap To True
Set ComAuthMethod Of hoImap To "XOAUTH2"
Get ComConnect Of hoImap "imap.gmail.com" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not establish the Gmail IMAP TLS connection."
Procedure_Return
End
Get ComStringOf Of hoToken "access_token" To sTemp1
Get ComLogin Of hoImap sMailbox sTemp1 To iSuccess
If (iSuccess <> True) Begin
Get ComDisconnect Of hoImap To iSuccess
Showln "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
Procedure_Return
End
// Authentication is finished before logging begins.
Send ComClearSessionLog To hoImap
Set ComKeepSessionLog Of hoImap To True
Get ComExamineMailbox Of hoImap "INBOX" To iSuccess
Get ComSessionLog Of hoImap To sDiagnostic
Set ComKeepSessionLog Of hoImap To False
Send ComClearSessionLog To hoImap
Get ComLogout Of hoImap To iSuccess
Get ComDisconnect Of hoImap To iSuccess
If (iSuccess <> True) Begin
Showln "EXAMINE failed. Inspect the captured post-login response privately."
Procedure_Return
End
// This limited log contains EXAMINE only, never the authentication exchange.
Showln sDiagnostic
End_Procedure