VB.NET Requires Chilkat v11.0.0+
VB.NET
Apply a Gmail label to search results in batches
See more GMail REST API Examples
Choose a Gmail search query and an existing label name. Resolve the label ID, collect all matching message IDs before changing any labels, then submit at most 1,000 IDs per batchModify call. Empty searches send no updates. For large mailboxes, spool IDs to persistent storage. Completed batches stay applied if a later request fails; adding the same label again is safe.
Authorize the application with https://www.googleapis.com/auth/gmail.modify. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-token.json")
If (success <> True) Then
Debug.WriteLine("Could not read gmail-token.json from the private working directory.")
Exit Sub
End If
If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
Debug.WriteLine("An OAuth access token is required.")
Exit Sub
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = token.StringOf("access_token")
Dim resp As New Chilkat.HttpResponse
Dim reply As New Chilkat.JsonObject
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/labels",resp)
If (resp.StatusCode = 0) Then
Debug.WriteLine("Gmail did not return an HTTP response.")
Exit Sub
End If
If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Debug.WriteLine("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine(reply.StringOf("error.message"))
End If
Exit Sub
End If
success = reply.Load(resp.BodyStr)
If (success <> True) Then
Debug.WriteLine("Expected a Gmail JSON response.")
Exit Sub
End If
If (reply.JsonTypeOf("labels") <> 4) Then
Debug.WriteLine("Expected a list of labels.")
Exit Sub
End If
Dim label As New Chilkat.JsonObject
success = reply.FindRecord2("labels","name","To review",True,label)
If (success <> True) Then
Debug.WriteLine("No label with that exact name. Check the name or create the label first.")
Exit Sub
End If
Dim labelId As New Chilkat.StringBuilder
success = label.StringOfSb("id",labelId)
If (success <> True) Then
Debug.WriteLine("Could not read the label ID.")
Exit Sub
End If
Dim ids As New Chilkat.StringTable
Dim baseUrl As New Chilkat.StringBuilder
baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=500")
Dim searchQuery As String = "has:attachment"
baseUrl.Append("&q=")
baseUrl.Append(http.UrlEncode(searchQuery))
Dim url As New Chilkat.StringBuilder
Dim pageToken As New Chilkat.StringBuilder
Dim more As Boolean = True
While (more = True)
url.SetString(baseUrl.GetAsString())
If (pageToken.Length > 0) Then
url.Append("&pageToken=")
url.Append(http.UrlEncode(pageToken.GetAsString()))
End If
success = http.HttpNoBody("GET",url.GetAsString(),resp)
If (resp.StatusCode = 0) Then
Debug.WriteLine("Gmail did not return an HTTP response.")
Exit Sub
End If
If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Debug.WriteLine("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine(reply.StringOf("error.message"))
End If
Exit Sub
End If
success = reply.Load(resp.BodyStr)
If (success <> True) Then
Debug.WriteLine("Expected a Gmail JSON response.")
Exit Sub
End If
' Gmail can omit messages when the search or mailbox is empty.
Dim count As Integer = reply.SizeOfArray("messages")
Dim i As Integer = 0
While (i < count)
reply.I = i
success = ids.Append(reply.StringOf("messages[i].id"))
If (success <> True) Then
Debug.WriteLine("Could not retain a message ID.")
Exit Sub
End If
i = i + 1
End While
more = False
If (reply.JsonTypeOf("nextPageToken") = 1) Then
pageToken.SetString(reply.StringOf("nextPageToken"))
more = pageToken.Length > 0
End If
End While
' Search is complete. Apply the label to each group and track confirmed progress.
Dim request As New Chilkat.JsonObject
Dim done As Integer = 0
While (done < ids.Count)
request.Clear()
request.UpdateString("addLabelIds[0]",labelId.GetAsString())
Dim n As Integer = 0
While ((n < 1000) And ((done + n) < ids.Count))
request.I = n
request.UpdateString("ids[i]",ids.StringAt(done + n))
n = n + 1
End While
success = http.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/batchModify",request,"application/json",resp)
If (resp.StatusCode = 0) Then
Debug.WriteLine("Gmail did not return an HTTP response.")
Exit Sub
End If
If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Debug.WriteLine("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If (success = True) Then
Debug.WriteLine(reply.StringOf("error.message"))
End If
Exit Sub
End If
done = done + n
Debug.WriteLine("Messages labeled so far: " & done)
End While
Debug.WriteLine("Total messages labeled: " & done)