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.
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
Dim url As New Chilkat.StringBuilder
url.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
url.Append("GMAIL_MESSAGE_ID")
url.Append("?format=raw")
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
If ((reply.JsonTypeOf("threadId") <> 1) Or (reply.StringOfEquals("threadId","",True) = True)) Then
Debug.WriteLine("Expected threadId in the response.")
Exit Sub
End If
If ((reply.JsonTypeOf("raw") <> 1) Or (reply.StringOfEquals("raw","",True) = True)) Then
Debug.WriteLine("Expected raw in the response.")
Exit Sub
End If
Dim threadId As String = reply.StringOf("threadId")
Dim originalMime As New Chilkat.BinData
success = reply.BytesOf("raw","base64url",originalMime)
If (success <> True) Then
Debug.WriteLine(reply.LastErrorText)
Exit Sub
End If
Dim original As New Chilkat.Email
success = original.SetFromMimeBd(originalMime)
If (success <> True) Then
Debug.WriteLine(original.LastErrorText)
Exit Sub
End If
Dim messageIdHeader As String = original.GetHeaderField("Message-ID")
If ((original.LastMethodSuccess <> True) Or (messageIdHeader = "")) Then
Debug.WriteLine("The original message needs a Message-ID for this threaded reply.")
Exit Sub
End If
Dim email As New Chilkat.Email
email.From = "Sender <sender@example.com>"
' Use the parsed Reply-To mailbox list when present, otherwise use From.
Dim replyAddress As String = original.ReplyTo
If (replyAddress = "") Then
replyAddress = original.From
End If
success = email.AddMultipleTo(replyAddress)
If (success <> True) Then
Debug.WriteLine(email.LastErrorText)
Exit Sub
End If
If (email.NumTo = 0) Then
Debug.WriteLine("The original message has no usable reply address.")
Exit Sub
End If
email.Subject = original.Subject
email.Charset = "utf-8"
email.Body = "Thank you for your message."
email.AddHeaderField("In-Reply-To",messageIdHeader)
Dim references As New Chilkat.StringBuilder
Dim previousReferences As String = original.GetHeaderField("References")
If ((original.LastMethodSuccess = True) And (previousReferences <> "")) Then
references.Append(previousReferences)
references.Append(" ")
End If
references.Append(messageIdHeader)
email.AddHeaderField("References",references.GetAsString())
Dim mime As New Chilkat.BinData
success = email.GetMimeBd(mime)
If (success <> True) Then
Debug.WriteLine(email.LastErrorText)
Exit Sub
End If
Dim request As New Chilkat.JsonObject
success = request.UpdateBd("raw","base64url",mime)
If (success <> True) Then
Debug.WriteLine(request.LastErrorText)
Exit Sub
End If
success = request.UpdateString("threadId",threadId)
If (success <> True) Then
Debug.WriteLine(request.LastErrorText)
Exit Sub
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
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("id") <> 1) Or (reply.StringOfEquals("id","",True) = True)) Then
Debug.WriteLine("Expected id in the response.")
Exit Sub
End If
Debug.WriteLine("Sent reply ID: " & reply.StringOf("id"))
Debug.WriteLine("Thread ID: " & reply.StringOf("threadId"))