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

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.

Google documentation

Chilkat VBScript Downloads

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

success = 0

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

set token = CreateObject("Chilkat.JsonObject")
success = token.LoadFile("gmail-token.json")
If (success <> 1) Then
    outFile.WriteLine("Could not read gmail-token.json from the private working directory.")
    WScript.Quit
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",1) = 1)) Then
    outFile.WriteLine("An OAuth access token is required.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = token.StringOf("access_token")
set resp = CreateObject("Chilkat.HttpResponse")
set reply = CreateObject("Chilkat.JsonObject")
set email = CreateObject("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 <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
End If

email.Subject = "Sample message"
email.Charset = "utf-8"
email.Body = "Hello from the Gmail API."
set fileData = CreateObject("Chilkat.BinData")
success = fileData.LoadFile("attachment.pdf")
If (success <> 1) Then
    outFile.WriteLine("Could not read attachment.pdf.")
    WScript.Quit
End If

success = email.AddAttachmentBd("attachment.pdf",fileData,"application/pdf")
If (success <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
End If

set mime = CreateObject("Chilkat.BinData")
success = email.GetMimeBd(mime)
If (success <> 1) Then
    outFile.WriteLine(email.LastErrorText)
    WScript.Quit
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
    outFile.WriteLine("Gmail did not return an HTTP response.")
    WScript.Quit
End If

If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
    outFile.WriteLine("Gmail HTTP " & resp.StatusCode)
    success = reply.Load(resp.BodyStr)
    If (success = 1) Then
        outFile.WriteLine(reply.StringOf("error.message"))
    End If

    WScript.Quit
End If

success = reply.Load(resp.BodyStr)
If (success <> 1) Then
    outFile.WriteLine("Expected a Gmail JSON response.")
    WScript.Quit
End If

If ((reply.JsonTypeOf("id") <> 1) Or (reply.StringOfEquals("id","",1) = 1)) Then
    outFile.WriteLine("Expected id in the response.")
    WScript.Quit
End If

outFile.WriteLine("Gmail message ID: " & reply.StringOf("id"))

outFile.Close