Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.0.0+

IMAP: Inspect LastResponse and LastResponseCode

See more IMAP Examples

Authenticate successfully, then examine an intentionally absent mailbox to demonstrate a server rejection without using an invalid password. Capture LastResponse and LastResponseCode immediately, before cleanup overwrites them.

A server may return a response code such as NONEXISTENT, but exact wording and codes vary. These properties are not a substitute for checking the method result: a failure before a command is sent can leave a stale response. Inspect diagnostic errors privately and avoid publishing authentication data.

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loToken
LOCAL lcMailbox
LOCAL loImap
LOCAL lcResponse
LOCAL lcResponseCode

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.

loToken = CreateObject('Chilkat.JsonObject')
lnSuccess = loToken.LoadFile("gmail-mail-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not read gmail-mail-token.json from the private working directory."
    RELEASE loToken
    CANCEL
ENDIF

IF ((loToken.JsonTypeOf("access_token") <> 1) OR (loToken.StringOfEquals("access_token","",1) = 1)) THEN
    ? "An OAuth access token is required. Authorize or refresh the account first."
    RELEASE loToken
    CANCEL
ENDIF

lcMailbox = "user@example.com"
loImap = CreateObject('Chilkat.Imap')
loImap.Ssl = 1
loImap.Port = 993
loImap.RequireSslCertVerify = 1
loImap.AuthMethod = "XOAUTH2"
lnSuccess = loImap.Connect("imap.gmail.com")
IF (lnSuccess <> 1) THEN
    ? "Could not establish the Gmail IMAP TLS connection."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

lnSuccess = loImap.Login(lcMailbox,loToken.StringOf("access_token"))
IF (lnSuccess <> 1) THEN
    loImap.Disconnect()
    ? "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

*  Choose a mailbox name that does not exist in this test account.
lnSuccess = loImap.ExamineMailbox("__example_nonexistent_mailbox__")
*  Read these immediately: logout and other commands replace the last response.
lcResponse = loImap.LastResponse
lcResponseCode = loImap.LastResponseCode
loImap.Logout()
loImap.Disconnect()
IF (lnSuccess = 1) THEN
    ? "That mailbox exists. Choose an absent name to demonstrate a server error."
    RELEASE loToken
    RELEASE loImap
    CANCEL
ENDIF

? "LastResponseCode: " + lcResponseCode
? "LastResponse: " + lcResponse
*  A local failure before a command is sent can leave an earlier response here.
*  Inspect LastErrorText privately when diagnosing transport or local failures.

RELEASE loToken
RELEASE loImap