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 VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-mail-token.json")
If (success <> 1) Then
outFile.WriteLine("Could not load the protocol token from the private working directory.")
WScript.Quit
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
outFile.WriteLine("Authorize or refresh the mailbox first.")
WScript.Quit
End If
mailbox = "user@example.com"
set imap = 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
outFile.WriteLine("Could not establish the Gmail IMAP TLS connection.")
WScript.Quit
End If
success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> 1) Then
success = imap.Disconnect()
outFile.WriteLine("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
WScript.Quit
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()
outFile.WriteLine("Gmail IMAP extensions are unavailable.")
WScript.Quit
End If
set folders = CreateObject("Chilkat.Mailboxes")
success = imap.MbxList(0,"","*",folders)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not list Gmail folders.")
WScript.Quit
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()
outFile.WriteLine("Expected one selectable folder with the \All special-use attribute. Check label visibility.")
WScript.Quit
End If
success = imap.ExamineMailbox(allMail)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Could not open All Mail read-only.")
WScript.Quit
End If
' This search uses Gmail syntax inside a quoted IMAP search argument.
query = "has:attachment from:sender@example.net"
set quoted = 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()
outFile.WriteLine("A search query must not contain CR or LF.")
WScript.Quit
End If
replacements = quoted.Replace("\","\\")
replacements = quoted.Replace("""","\""")
set joined1 = CreateObject("Chilkat.StringBuilder")
success = joined1.Append("X-GM-RAW """)
success = joined1.Append(quoted.GetAsString())
success = joined1.Append("""")
criteria = joined1.GetAsString()
set found = CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx(criteria,1,found)
If (success <> 1) Then
success = imap.Logout()
success = imap.Disconnect()
outFile.WriteLine("Gmail search failed.")
WScript.Quit
End If
' Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
outFile.WriteLine("Matching messages: " & found.Count)
outFile.WriteLine("UID set: " & found.ToCompactString())
success = imap.Logout()
success = imap.Disconnect()
outFile.Close