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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim token As ChilkatJsonObject
token.Initialize
success = token.LoadFile("gmail-token.json")
If success <> True Then
Log("Could not read gmail-token.json from the private working directory.")
Return
End If
If (token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token", "", True) = True) Then
Log("An OAuth access token is required.")
Return
End If
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.AuthToken = token.StringOf("access_token")
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim reply As ChilkatJsonObject
reply.Initialize
Dim url As ChilkatStringBuilder
url.Initialize
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
Log("Gmail did not return an HTTP response.")
Return
End If
If (success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Log("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If success = True Then
Log(reply.StringOf("error.message"))
End If
Return
End If
success = reply.Load(resp.BodyStr)
If success <> True Then
Log("Expected a Gmail JSON response.")
Return
End If
If (reply.JsonTypeOf("threadId") <> 1) Or (reply.StringOfEquals("threadId", "", True) = True) Then
Log("Expected threadId in the response.")
Return
End If
If (reply.JsonTypeOf("raw") <> 1) Or (reply.StringOfEquals("raw", "", True) = True) Then
Log("Expected raw in the response.")
Return
End If
Dim threadId As String = reply.StringOf("threadId")
Dim originalMime As ChilkatBinData
originalMime.Initialize
success = reply.BytesOf("raw", "base64url", originalMime)
If success <> True Then
Log(reply.LastErrorText)
Return
End If
Dim original As ChilkatEmail
original.Initialize
success = original.SetFromMimeBd(originalMime)
If success <> True Then
Log(original.LastErrorText)
Return
End If
Dim messageIdHeader As String = original.GetHeaderField("Message-ID")
If (original.LastMethodSuccess <> True) Or (messageIdHeader = "") Then
Log("The original message needs a Message-ID for this threaded reply.")
Return
End If
Dim email As ChilkatEmail
email.Initialize
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
Log(email.LastErrorText)
Return
End If
If email.NumTo = 0 Then
Log("The original message has no usable reply address.")
Return
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 ChilkatStringBuilder
references.Initialize
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 ChilkatBinData
mime.Initialize
success = email.GetMimeBd(mime)
If success <> True Then
Log(email.LastErrorText)
Return
End If
Dim request As ChilkatJsonObject
request.Initialize
success = request.UpdateBd("raw", "base64url", mime)
If success <> True Then
Log(request.LastErrorText)
Return
End If
success = request.UpdateString("threadId", threadId)
If success <> True Then
Log(request.LastErrorText)
Return
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
Log("Gmail did not return an HTTP response.")
Return
End If
If (success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300) Then
Log("Gmail HTTP " & resp.StatusCode)
success = reply.Load(resp.BodyStr)
If success = True Then
Log(reply.StringOf("error.message"))
End If
Return
End If
success = reply.Load(resp.BodyStr)
If success <> True Then
Log("Expected a Gmail JSON response.")
Return
End If
If (reply.JsonTypeOf("id") <> 1) Or (reply.StringOfEquals("id", "", True) = True) Then
Log("Expected id in the response.")
Return
End If
Log("Sent reply ID: " & reply.StringOf("id"))
Log("Thread ID: " & reply.StringOf("threadId"))