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

Reply to a message in its existing Gmail thread

See more GMail REST API Examples

Live send. Supply the Gmail ID of a received message and use an authorized From address. Fetch its raw MIME so Chilkat.Email can parse encoded headers and recipient addresses. Send a plain-text reply to Reply-To, falling back to From. The reply goes to the original sender or reply address.

The request supplies Gmail’s threadId and matching Subject. The MIME In-Reply-To contains the original RFC Message-ID. References preserves the conversation ancestry and appends that Message-ID. Select a message with a valid Message-ID and review its recipient before running this sending example. Authorize both gmail.readonly and gmail.send, or use gmail.modify as shown below.

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")
set url = Server.CreateObject("Chilkat.StringBuilder")
success = url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
success = url.Append("GMAIL_MESSAGE_ID")
success = url.Append("?format=raw")
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

If ((reply.JsonTypeOf("threadId") <> 1) Or (reply.StringOfEquals("threadId","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected threadId in the response.") & "</pre>"
    Response.End
End If

If ((reply.JsonTypeOf("raw") <> 1) Or (reply.StringOfEquals("raw","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected raw in the response.") & "</pre>"
    Response.End
End If

threadId = reply.StringOf("threadId")
set originalMime = Server.CreateObject("Chilkat.BinData")
success = reply.BytesOf("raw","base64url",originalMime)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( reply.LastErrorText) & "</pre>"
    Response.End
End If

set original = Server.CreateObject("Chilkat.Email")
success = original.SetFromMimeBd(originalMime)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( original.LastErrorText) & "</pre>"
    Response.End
End If

messageIdHeader = original.GetHeaderField("Message-ID")
If ((original.LastMethodSuccess <> 1) Or (messageIdHeader = "")) Then
    Response.Write "<pre>" & Server.HTMLEncode( "The original message needs a Message-ID for this threaded reply.") & "</pre>"
    Response.End
End If

set email = Server.CreateObject("Chilkat.Email")
email.From = "Sender <sender@example.com>"
'  Use the parsed Reply-To mailbox list when present, otherwise use From.
replyAddress = original.ReplyTo
If (replyAddress = "") Then
    replyAddress = original.From
End If

success = email.AddMultipleTo(replyAddress)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( email.LastErrorText) & "</pre>"
    Response.End
End If

If (email.NumTo = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "The original message has no usable reply address.") & "</pre>"
    Response.End
End If

email.Subject = original.Subject
email.Charset = "utf-8"
email.Body = "Thank you for your message."
email.AddHeaderField "In-Reply-To",messageIdHeader
set references = Server.CreateObject("Chilkat.StringBuilder")
previousReferences = original.GetHeaderField("References")
If ((original.LastMethodSuccess = 1) And (previousReferences <> "")) Then
    success = references.Append(previousReferences)
    success = references.Append(" ")
End If

success = references.Append(messageIdHeader)
email.AddHeaderField "References",references.GetAsString()
set mime = Server.CreateObject("Chilkat.BinData")
success = email.GetMimeBd(mime)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( email.LastErrorText) & "</pre>"
    Response.End
End If

ERROR: ASP example cannot use request or response as a variable name.
success = request.UpdateBd("raw","base64url",mime)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( request.LastErrorText) & "</pre>"
    Response.End
End If

success = request.UpdateString("threadId",threadId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( request.LastErrorText) & "</pre>"
    Response.End
End If

success = http.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/send",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

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("id") <> 1) Or (reply.StringOfEquals("id","",1) = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected id in the response.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Sent reply ID: " & reply.StringOf("id")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Thread ID: " & reply.StringOf("threadId")) & "</pre>"

%>
</body>
</html>