Sample code for 30+ languages & platforms
AutoIt 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 AutoIt Downloads

AutoIt
Local $bSuccess = False

;  Assumes Chilkat is unlocked. See Global Unlock Sample.

$oToken = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oToken.LoadFile("gmail-token.json")
If ($bSuccess <> True) Then
    ConsoleWrite("Could not read gmail-token.json from the private working directory." & @CRLF)
    Exit
EndIf

If (($oToken.JsonTypeOf("access_token") <> 1) Or ($oToken.StringOfEquals("access_token","",True) = True)) Then
    ConsoleWrite("An OAuth access token is required." & @CRLF)
    Exit
EndIf

$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.AuthToken = $oToken.StringOf("access_token")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$oUrl = ObjCreate("Chilkat.StringBuilder")
$oUrl.Append("https://gmail.googleapis.com/gmail/v1/users/me/messages/")
$oUrl.Append("GMAIL_MESSAGE_ID")
$oUrl.Append("?format=raw")
$bSuccess = $oHttp.HttpNoBody("GET",$oUrl.GetAsString(),$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
    $bSuccess = $oReply.Load($oResp.BodyStr)
    If ($bSuccess = True) Then
        ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
    EndIf

    Exit
EndIf

$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
    Exit
EndIf

If (($oReply.JsonTypeOf("threadId") <> 1) Or ($oReply.StringOfEquals("threadId","",True) = True)) Then
    ConsoleWrite("Expected threadId in the response." & @CRLF)
    Exit
EndIf

If (($oReply.JsonTypeOf("raw") <> 1) Or ($oReply.StringOfEquals("raw","",True) = True)) Then
    ConsoleWrite("Expected raw in the response." & @CRLF)
    Exit
EndIf

Local $sThreadId = $oReply.StringOf("threadId")
$oOriginalMime = ObjCreate("Chilkat.BinData")
$bSuccess = $oReply.BytesOf("raw","base64url",$oOriginalMime)
If ($bSuccess <> True) Then
    ConsoleWrite($oReply.LastErrorText & @CRLF)
    Exit
EndIf

$oOriginal = ObjCreate("Chilkat.Email")
$bSuccess = $oOriginal.SetFromMimeBd($oOriginalMime)
If ($bSuccess <> True) Then
    ConsoleWrite($oOriginal.LastErrorText & @CRLF)
    Exit
EndIf

Local $sMessageIdHeader = $oOriginal.GetHeaderField("Message-ID")
If (($oOriginal.LastMethodSuccess <> True) Or ($sMessageIdHeader = "")) Then
    ConsoleWrite("The original message needs a Message-ID for this threaded reply." & @CRLF)
    Exit
EndIf

$oEmail = ObjCreate("Chilkat.Email")
$oEmail.From = "Sender <sender@example.com>"
;  Use the parsed Reply-To mailbox list when present, otherwise use From.
Local $sReplyAddress = $oOriginal.ReplyTo
If ($sReplyAddress = "") Then
    $sReplyAddress = $oOriginal.From
EndIf

$bSuccess = $oEmail.AddMultipleTo($sReplyAddress)
If ($bSuccess <> True) Then
    ConsoleWrite($oEmail.LastErrorText & @CRLF)
    Exit
EndIf

If ($oEmail.NumTo = 0) Then
    ConsoleWrite("The original message has no usable reply address." & @CRLF)
    Exit
EndIf

$oEmail.Subject = $oOriginal.Subject
$oEmail.Charset = "utf-8"
$oEmail.Body = "Thank you for your message."
$oEmail.AddHeaderField "In-Reply-To",$sMessageIdHeader
$oReferences = ObjCreate("Chilkat.StringBuilder")
Local $sPreviousReferences = $oOriginal.GetHeaderField("References")
If (($oOriginal.LastMethodSuccess = True) And ($sPreviousReferences <> "")) Then
    $oReferences.Append($sPreviousReferences)
    $oReferences.Append(" ")
EndIf

$oReferences.Append($sMessageIdHeader)
$oEmail.AddHeaderField "References",$oReferences.GetAsString()
$oMime = ObjCreate("Chilkat.BinData")
$bSuccess = $oEmail.GetMimeBd($oMime)
If ($bSuccess <> True) Then
    ConsoleWrite($oEmail.LastErrorText & @CRLF)
    Exit
EndIf

$oRequest = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oRequest.UpdateBd("raw","base64url",$oMime)
If ($bSuccess <> True) Then
    ConsoleWrite($oRequest.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oRequest.UpdateString("threadId",$sThreadId)
If ($bSuccess <> True) Then
    ConsoleWrite($oRequest.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oHttp.HttpJson("POST","https://gmail.googleapis.com/gmail/v1/users/me/messages/send",$oRequest,"application/json",$oResp)
If ($oResp.StatusCode = 0) Then
    ConsoleWrite("Gmail did not return an HTTP response." & @CRLF)
    Exit
EndIf

If (($bSuccess <> True) Or ($oResp.StatusCode < 200) Or ($oResp.StatusCode >= 300)) Then
    ConsoleWrite("Gmail HTTP " & $oResp.StatusCode & @CRLF)
    $bSuccess = $oReply.Load($oResp.BodyStr)
    If ($bSuccess = True) Then
        ConsoleWrite($oReply.StringOf("error.message") & @CRLF)
    EndIf

    Exit
EndIf

$bSuccess = $oReply.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
    ConsoleWrite("Expected a Gmail JSON response." & @CRLF)
    Exit
EndIf

If (($oReply.JsonTypeOf("id") <> 1) Or ($oReply.StringOfEquals("id","",True) = True)) Then
    ConsoleWrite("Expected id in the response." & @CRLF)
    Exit
EndIf

ConsoleWrite("Sent reply ID: " & $oReply.StringOf("id") & @CRLF)
ConsoleWrite("Thread ID: " & $oReply.StringOf("threadId") & @CRLF)