Gmail IMAP: Search with Gmail Search Syntax
See more GMail SMTP/IMAP/POP Examples
Search with Gmail operators through X-GM-RAW, using QueryMbx to obtain UIDs. The example discovers All Mail and opens it read-only. It quotes the query for the IMAP command rather than URL-encoding it.
IMAP search is limited to the selected mailbox. All Mail normally covers inbox and archived messages, but excludes Spam and Trash; a Gmail search operator cannot expand an IMAP search beyond its selected mailbox. An empty result is successful. Returned IMAP UIDs are mailbox-specific and are not Gmail message IDs. Persist them only with their mailbox and UIDVALIDITY.
Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.
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 load the protocol token 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( "Authorize or refresh the mailbox 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, scope, expiry, and account policy.") & "</pre>"
Response.End
End If
capabilities = imap.Capability()
If ((imap.LastMethodSuccess <> 1) Or (imap.HasCapability("X-GM-EXT-1",capabilities) <> 1)) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "Gmail IMAP extensions are unavailable.") & "</pre>"
Response.End
End If
set folders = Server.CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "Could not list Gmail folders.") & "</pre>"
Response.End
End If
allMail = ""
folderMatches = 0
f = 0
Do While (f < folders.Count)
If ((folders.HasFlag(f,"\All") = 1) And (folders.IsSelectable(f) = 1)) Then
allMail = folders.GetName(f)
folderMatches = folderMatches + 1
End If
f = f + 1
Loop
If (folderMatches <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "Expected one selectable folder with the \All special-use attribute. Check label visibility.") & "</pre>"
Response.End
End If
success = imap.ExamineMailbox(allMail)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "Could not open All Mail read-only.") & "</pre>"
Response.End
End If
' This search uses Gmail syntax inside a quoted IMAP search argument.
query = "has:attachment from:sender@example.net"
set quoted = Server.CreateObject("Chilkat.StringBuilder")
success = quoted.Append(query)
If ((quoted.Contains(vbCr,1) = 1) Or (quoted.Contains(vbLf,1) = 1)) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "A search query must not contain CR or LF.") & "</pre>"
Response.End
End If
replacements = quoted.Replace("\","\\")
replacements = quoted.Replace("""","\""")
set joined1 = Server.CreateObject("Chilkat.StringBuilder")
success = joined1.Append("X-GM-RAW """)
success = joined1.Append(quoted.GetAsString())
success = joined1.Append("""")
criteria = joined1.GetAsString()
set found = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx(criteria,1,found)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
Response.Write "<pre>" & Server.HTMLEncode( "Gmail search failed.") & "</pre>"
Response.End
End If
' Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
Response.Write "<pre>" & Server.HTMLEncode( "Matching messages: " & found.Count) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "UID set: " & found.ToCompactString()) & "</pre>"
success = imap.Logout()
success = imap.Disconnect()
%>
</body>
</html>