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

Send email to blind-copy recipients with Gmail

See more GMail REST API Examples

Live send. Replace all sample addresses. Gmail takes its recipients from the submitted MIME To, Cc, and Bcc headers. Use AddBcc on a new Email and keep the Bcc header when serializing it for Gmail. Treat the submission as private because it contains the blind-copy addresses.

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoToken
    Handle hoHttp
    Variant vResp
    Handle hoResp
    Handle hoReply
    Handle hoEmail
    Variant vMime
    Handle hoMime
    Variant vRequest
    Handle hoRequest
    String sTemp1
    Integer iTemp1
    Integer iTemp2
    Boolean bTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatJsonObject)) To hoToken
    If (Not(IsComObjectCreated(hoToken))) Begin
        Send CreateComObject of hoToken
    End
    Get ComLoadFile Of hoToken "gmail-token.json" To iSuccess
    If (iSuccess <> True) Begin
        Showln "Could not read gmail-token.json from the private working directory."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
    Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "An OAuth access token is required."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    Get ComStringOf Of hoToken "access_token" To sTemp1
    Set ComAuthToken Of hoHttp To sTemp1
    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    //  Use the authorized mailbox or one of its configured send-as addresses.
    Set ComFrom Of hoEmail To "Sender <sender@example.com>"
    Get ComAddTo Of hoEmail "Recipient" "recipient@example.net" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Set ComSubject Of hoEmail To "Sample message"
    Set ComCharset Of hoEmail To "utf-8"
    Set ComBody Of hoEmail To "Hello from the Gmail API."
    //  Gmail needs the blind recipient in the submitted MIME; use the normal Chilkat 11.x Bcc behavior.
    Get ComAddBcc Of hoEmail "Blind recipient" "blind@example.org" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatBinData)) To hoMime
    If (Not(IsComObjectCreated(hoMime))) Begin
        Send CreateComObject of hoMime
    End
    Get pvComObject of hoMime to vMime
    Get ComGetMimeBd Of hoEmail vMime To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoRequest
    If (Not(IsComObjectCreated(hoRequest))) Begin
        Send CreateComObject of hoRequest
    End
    Get pvComObject of hoMime to vMime
    Get ComUpdateBd Of hoRequest "raw" "base64url" vMime To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoRequest To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoRequest to vRequest
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://gmail.googleapis.com/gmail/v1/users/me/messages/send" vRequest "application/json" vResp To iSuccess
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 = 0) Begin
        Showln "Gmail did not return an HTTP response."
        Procedure_Return
    End

    Get ComStatusCode Of hoResp To iTemp1
    Get ComStatusCode Of hoResp To iTemp2
    If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Gmail HTTP " iTemp1
        Get ComBodyStr Of hoResp To sTemp1
        Get ComLoad Of hoReply sTemp1 To iSuccess
        If (iSuccess = True) Begin
            Get ComStringOf Of hoReply "error.message" To sTemp1
            Showln sTemp1
        End

        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoReply sTemp1 To iSuccess
    If (iSuccess <> True) Begin
        Showln "Expected a Gmail JSON response."
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoReply "id" To iTemp1
    Get ComStringOfEquals Of hoReply "id" "" True To bTemp1
    If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
        Showln "Expected id in the response."
        Procedure_Return
    End

    Get ComStringOf Of hoReply "id" To sTemp1
    Showln "Gmail message ID: " sTemp1


End_Procedure