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 Go Downloads
success := false
var replacements int
// Assumes Chilkat is unlocked. See Global Unlock Sample.
token := chilkat.NewJsonObject()
success = token.LoadFile("gmail-mail-token.json")
if success != true {
fmt.Println("Could not load the protocol token from the private working directory.")
token.DisposeJsonObject()
return
}
if (token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true) {
fmt.Println("Authorize or refresh the mailbox first.")
token.DisposeJsonObject()
return
}
mailbox := "user@example.com"
imap := chilkat.NewImap()
imap.SetSsl(true)
imap.SetPort(993)
imap.SetRequireSslCertVerify(true)
imap.SetAuthMethod("XOAUTH2")
success = imap.Connect("imap.gmail.com")
if success != true {
fmt.Println("Could not establish the Gmail IMAP TLS connection.")
token.DisposeJsonObject()
imap.DisposeImap()
return
}
success = imap.Login(mailbox,*token.StringOf("access_token"))
if success != true {
imap.Disconnect()
fmt.Println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
token.DisposeJsonObject()
imap.DisposeImap()
return
}
capabilities := imap.Capability()
if (imap.LastMethodSuccess() != true) || (imap.HasCapability("X-GM-EXT-1",*capabilities) != true) {
imap.Logout()
imap.Disconnect()
fmt.Println("Gmail IMAP extensions are unavailable.")
token.DisposeJsonObject()
imap.DisposeImap()
return
}
folders := chilkat.NewMailboxes()
success = imap.MbxList(false,"","*",folders)
if success != true {
imap.Logout()
imap.Disconnect()
fmt.Println("Could not list Gmail folders.")
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
return
}
var allMail *string = new(string)
*allMail = ""
folderMatches := 0
f := 0
for (f < folders.Count()) {
if (folders.HasFlag(f,"\\All") == true) && (folders.IsSelectable(f) == true) {
allMail = folders.GetName(f)
folderMatches = folderMatches + 1
}
f = f + 1
}
if folderMatches != 1 {
imap.Logout()
imap.Disconnect()
fmt.Println("Expected one selectable folder with the \\All special-use attribute. Check label visibility.")
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
return
}
success = imap.ExamineMailbox(*allMail)
if success != true {
imap.Logout()
imap.Disconnect()
fmt.Println("Could not open All Mail read-only.")
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
return
}
// This search uses Gmail syntax inside a quoted IMAP search argument.
query := "has:attachment from:sender@example.net"
quoted := chilkat.NewStringBuilder()
quoted.Append(query)
if (quoted.Contains("\r",true) == true) || (quoted.Contains("\n",true) == true) {
imap.Logout()
imap.Disconnect()
fmt.Println("A search query must not contain CR or LF.")
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
quoted.DisposeStringBuilder()
return
}
replacements = quoted.Replace("\\","\\\\")
replacements = quoted.Replace("\"","\\\"")
joined1 := chilkat.NewStringBuilder()
joined1.Append("X-GM-RAW \"")
joined1.Append(*quoted.GetAsString())
joined1.Append("\"")
criteria := joined1.GetAsString()
found := chilkat.NewMessageSet()
success = imap.QueryMbx(*criteria,true,found)
if success != true {
imap.Logout()
imap.Disconnect()
fmt.Println("Gmail search failed.")
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
quoted.DisposeStringBuilder()
joined1.DisposeStringBuilder()
found.DisposeMessageSet()
return
}
// Preserve UIDs as text. They belong to this mailbox and its UIDVALIDITY.
fmt.Println("Matching messages: ", found.Count())
fmt.Println("UID set: ", *found.ToCompactString())
imap.Logout()
imap.Disconnect()
token.DisposeJsonObject()
imap.DisposeImap()
folders.DisposeMailboxes()
quoted.DisposeStringBuilder()
joined1.DisposeStringBuilder()
found.DisposeMessageSet()