Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.0.0+

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.

Google documentation

Chilkat Classic ASP Downloads

Classic ASP
<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-token.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read gmail-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.") & "</pre>"
    Response.End
End If

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = token.StringOf("access_token")
set resp = Server.CreateObject("Chilkat.HttpResponse")
set reply = Server.CreateObject("Chilkat.JsonObject")
success = http.HttpNoBody("GET","https://gmail.googleapis.com/gmail/v1/users/me/labels",resp)
If (resp.StatusCode = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
    Response.End
End If

If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
    success = reply.Load(resp.BodyStr)
    If (success = 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
    End If

    Response.End
End If

success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a Gmail JSON response.") & "</pre>"
    Response.End
End If

If (reply.JsonTypeOf("labels") <> 4) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a list of labels.") & "</pre>"
    Response.End
End If

set label = Server.CreateObject("Chilkat.JsonObject")
success = reply.FindRecord2("labels","name","To review",1,label)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No label with that exact name. Check the name or create the label first.") & "</pre>"
    Response.End
End If

set labelId = Server.CreateObject("Chilkat.StringBuilder")
success = label.StringOfSb("id",labelId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the label ID.") & "</pre>"
    Response.End
End If

set ids = Server.CreateObject("Chilkat.StringTable")
set baseUrl = Server.CreateObject("Chilkat.StringBuilder")
success = baseUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages?maxResults=500")
searchQuery = "has:attachment"
success = baseUrl.Append("&q=")
success = baseUrl.Append(http.UrlEncode(searchQuery))
set url = Server.CreateObject("Chilkat.StringBuilder")
set pageToken = Server.CreateObject("Chilkat.StringBuilder")
more = 1
Do While (more = 1)
    success = url.SetString(baseUrl.GetAsString())
    If (pageToken.Length > 0) Then
        success = url.Append("&pageToken=")
        success = url.Append(http.UrlEncode(pageToken.GetAsString()))
    End If

    success = http.HttpNoBody("GET",url.GetAsString(),resp)
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
        Response.End
    End If

    If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
        success = reply.Load(resp.BodyStr)
        If (success = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
        End If

        Response.End
    End If

    success = reply.Load(resp.BodyStr)
    If (success <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Expected a Gmail JSON response.") & "</pre>"
        Response.End
    End If

    '  Gmail can omit messages when the search or mailbox is empty.
    count = reply.SizeOfArray("messages")
    i = 0
    Do While (i < count)
        reply.I = i
        success = ids.Append(reply.StringOf("messages[i].id"))
        If (success <> 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( "Could not retain a message ID.") & "</pre>"
            Response.End
        End If

        i = i + 1
    Loop
    more = 0
    If (reply.JsonTypeOf("nextPageToken") = 1) Then
        success = pageToken.SetString(reply.StringOf("nextPageToken"))
        more = pageToken.Length > 0
    End If

Loop
'  Search is complete. Apply the label to each group and track confirmed progress.
ERROR: ASP example cannot use request or response as a variable name.
done = 0
Do While (done < ids.Count)
    request.Clear 
    success = request.UpdateString("addLabelIds[0]",labelId.GetAsString())
    n = 0
    Do While ((n < 1000) And ((done + n) < ids.Count))
ERROR: ASP example cannot use request or response as a variable name.
        success = request.UpdateString("ids[i]",ids.StringAt(done + n))
        n = n + 1
    Loop
    success = http.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/batchModify",request,"application/json",resp)
    If (resp.StatusCode = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Gmail did not return an HTTP response.") & "</pre>"
        Response.End
    End If

    If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Gmail HTTP " & resp.StatusCode) & "</pre>"
        success = reply.Load(resp.BodyStr)
        If (success = 1) Then
            Response.Write "<pre>" & Server.HTMLEncode( reply.StringOf("error.message")) & "</pre>"
        End If

        Response.End
    End If

    done = done + n
    Response.Write "<pre>" & Server.HTMLEncode( "Messages labeled so far: " & done) & "</pre>"
Loop
Response.Write "<pre>" & Server.HTMLEncode( "Total messages labeled: " & done) & "</pre>"

%>
</body>
</html>