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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    String sMailbox
    Handle hoImap
    String sResponse
    String sResponseCode
    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

    //  Choose a mailbox name that does not exist in this test account.
    Get ComExamineMailbox Of hoImap "__example_nonexistent_mailbox__" To iSuccess
    //  Read these immediately: logout and other commands replace the last response.
    Get ComLastResponse Of hoImap To sResponse
    Get ComLastResponseCode Of hoImap To sResponseCode
    Get ComLogout Of hoImap To iSuccess
    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = True) Begin
        Showln "That mailbox exists. Choose an absent name to demonstrate a server error."
        Procedure_Return
    End

    Showln "LastResponseCode: " sResponseCode
    Showln "LastResponse: " sResponse
    //  A local failure before a command is sent can leave an earlier response here.
    //  Inspect LastErrorText privately when diagnosing transport or local failures.


End_Procedure