Gmail IMAP: Connect with an App Password
Authenticate to Gmail over implicit TLS on port 993, then open INBOX read-only and report its message count. No message is downloaded or changed. This replaces ordinary-password login and removes authentication-session logging.
Personal Gmail IMAP is always enabled. Workspace administrators can restrict IMAP access. For OAuth2, use Gmail IMAP token authentication.
Use an account-specific app password generated after enabling 2-Step Verification, loaded from protected configuration. A normal Google password will not work. App passwords may be unavailable under account or Workspace policy; OAuth2 is preferred. Changing the Google account password revokes app passwords. The obsolete less-secure-apps switch is not a setup option. Google app-password requirements.
See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.
Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
string ls_Mailbox
oleobject loo_Imap
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
ls_Mailbox = "user@example.com"
loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
destroy loo_Imap
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Imap.Ssl = 1
loo_Imap.Port = 993
loo_Imap.RequireSslCertVerify = 1
li_Success = loo_Imap.Connect("imap.gmail.com")
if li_Success <> 1 then
Write-Debug "Could not establish the Gmail IMAP TLS connection."
destroy loo_Imap
return
end if
li_Success = loo_Imap.Login(ls_Mailbox,"YOUR_16_CHARACTER_APP_PASSWORD")
if li_Success <> 1 then
loo_Imap.Disconnect()
Write-Debug "IMAP authentication failed. Check the app password, mailbox, and account policy."
destroy loo_Imap
return
end if
// EXAMINE is read-only; no message is downloaded, marked read, moved, or deleted.
li_Success = loo_Imap.ExamineMailbox("INBOX")
if li_Success <> 1 then
loo_Imap.Logout()
loo_Imap.Disconnect()
Write-Debug "Could not examine INBOX."
destroy loo_Imap
return
end if
Write-Debug "Messages in INBOX: " + string(loo_Imap.NumMessages)
li_Success = loo_Imap.Logout()
loo_Imap.Disconnect()
if li_Success <> 1 then
Write-Debug "The read-only operation completed, but IMAP logout failed."
end if
destroy loo_Imap