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

Unicode C++
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkEmailW.h>
#include <CkBinDataW.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    CkJsonObjectW token;
    success = token.LoadFile(L"gmail-token.json");
    if (success != true) {
        wprintf(L"Could not read gmail-token.json from the private working directory.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"An OAuth access token is required.\n");
        return;
    }

    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    http.put_AuthToken(token.stringOf(L"access_token"));
    CkHttpResponseW resp;
    CkJsonObjectW reply;
    CkEmailW email;
    //  Use the authorized mailbox or one of its configured send-as addresses.
    email.put_From(L"Sender <sender@example.com>");
    success = email.AddTo(L"Recipient",L"recipient@example.net");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    email.put_Subject(L"Sample message");
    email.put_Charset(L"utf-8");
    email.put_Body(L"Hello from the Gmail API.");
    CkBinDataW fileData;
    success = fileData.LoadFile(L"attachment.pdf");
    if (success != true) {
        wprintf(L"Could not read attachment.pdf.\n");
        return;
    }

    success = email.AddAttachmentBd(L"attachment.pdf",fileData,L"application/pdf");
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    CkBinDataW mime;
    success = email.GetMimeBd(mime);
    if (success != true) {
        wprintf(L"%s\n",email.lastErrorText());
        return;
    }

    success = http.HttpBd(L"POST",L"https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media",mime,L"message/rfc822",resp);
    if (resp.get_StatusCode() == 0) {
        wprintf(L"Gmail did not return an HTTP response.\n");
        return;
    }

    if ((success != true) || (resp.get_StatusCode() < 200) || (resp.get_StatusCode() >= 300)) {
        wprintf(L"Gmail HTTP %d\n",resp.get_StatusCode());
        success = reply.Load(resp.bodyStr());
        if (success == true) {
            wprintf(L"%s\n",reply.stringOf(L"error.message"));
        }

        return;
    }

    success = reply.Load(resp.bodyStr());
    if (success != true) {
        wprintf(L"Expected a Gmail JSON response.\n");
        return;
    }

    if ((reply.JsonTypeOf(L"id") != 1) || (reply.StringOfEquals(L"id",L"",true) == true)) {
        wprintf(L"Expected id in the response.\n");
        return;
    }

    wprintf(L"Gmail message ID: %s\n",reply.stringOf(L"id"));
    }