Xojo Plugin Requires Chilkat v11.0.0+
Xojo Plugin
Send a Gmail message with a file attachment
See more GMail REST API Examples
Live send. Prepare a small file named attachment.pdf and replace the sample addresses. Add the file as a MIME attachment and upload the complete message as message/rfc822. The response is a Gmail Message. For larger uploads or unreliable connections, see Google’s resumable upload guidance.
Authorize the application with https://www.googleapis.com/auth/gmail.send. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat Xojo Plugin Downloads
Dim success As Boolean
success = 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
System.DebugLog("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
System.DebugLog("An OAuth access token is required.")
Return
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 email As New Chilkat.Email
// Use the authorized mailbox or one of its configured send-as addresses.
email.From = "Sender <sender@example.com>"
success = email.AddTo("Recipient","recipient@example.net")
If (success <> True) Then
System.DebugLog(email.LastErrorText)
Return
End If
email.Subject = "Sample message"
email.Charset = "utf-8"
email.Body = "Hello from the Gmail API."
Dim fileData As New Chilkat.BinData
success = fileData.LoadFile("attachment.pdf")
If (success <> True) Then
System.DebugLog("Could not read attachment.pdf.")
Return
End If
success = email.AddAttachmentBd("attachment.pdf",fileData,"application/pdf")
If (success <> True) Then
System.DebugLog(email.LastErrorText)
Return
End If
Dim mime As New Chilkat.BinData
success = email.GetMimeBd(mime)
If (success <> True) Then
System.DebugLog(email.LastErrorText)
Return
End If
success = http.HttpBd("POST","https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media",mime,"message/rfc822",resp)
If (resp.StatusCode = 0) Then
System.DebugLog("Gmail did not return an HTTP response.")
Return
End If
If ((success <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
System.DebugLog("Gmail HTTP " + Str(resp.StatusCode))
success = reply.Load(resp.BodyStr)
If (success = True) Then
System.DebugLog(reply.StringOf("error.message"))
End If
Return
End If
success = reply.Load(resp.BodyStr)
If (success <> True) Then
System.DebugLog("Expected a Gmail JSON response.")
Return
End If
If ((reply.JsonTypeOf("id") <> 1) Or (reply.StringOfEquals("id","",True) = True)) Then
System.DebugLog("Expected id in the response.")
Return
End If
System.DebugLog("Gmail message ID: " + reply.StringOf("id"))