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.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = Server.CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read gmail-mail-token.json from the private working directory.") & "</pre>"
Response.End
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "An OAuth access token is required. Authorize or refresh the account first.") & "</pre>"
Response.End
End If
mailbox = "user@example.com"
set imap = Server.CreateObject("Chilkat.Imap")
imap.Ssl = 1
imap.Port = 993
imap.RequireSslCertVerify = 1
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not establish the Gmail IMAP TLS connection.") & "</pre>"
Response.End
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.") & "</pre>"
Response.End
End If
' Choose a mailbox name that does not exist in this test account.
success = imap.ExamineMailbox("__example_nonexistent_mailbox__")
' Read these immediately: logout and other commands replace the last response.
ERROR: ASP example cannot use request or response as a variable name.
responseCode = imap.LastResponseCode
success = imap.Logout()
success = imap.Disconnect()
If (success = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "That mailbox exists. Choose an absent name to demonstrate a server error.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "LastResponseCode: " & responseCode) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "LastResponse: " & response) & "</pre>"
' A local failure before a command is sent can leave an earlier response here.
' Inspect LastErrorText privately when diagnosing transport or local failures.
%>
</body>
</html>