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

Inspect the MIME parts of a Gmail message

See more GMail REST API Examples

Supply a Gmail message ID and request format=full. Walk the root payload and every nested parts array, including a single-part root. For each part, show its MIME type, filename, and size. A body.attachmentId can be passed to the attachment download example. Inline body.data is already included as base64url.

Authorize the application with https://www.googleapis.com/auth/gmail.readonly. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.

Google documentation

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'gmail-token.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not read gmail-token.json from the private working directory.'
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'An OAuth access token is required.'
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OASetProperty @http, 'AuthToken', @sTmp0
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    DECLARE @reply int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @reply OUT

    DECLARE @url int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @url OUT

    EXEC sp_OAMethod @url, 'Append', @success OUT, 'https://gmail.googleapis.com/gmail/v1/users/me/messages/'
    EXEC sp_OAMethod @url, 'Append', @success OUT, 'GMAIL_MESSAGE_ID'
    EXEC sp_OAMethod @url, 'Append', @success OUT, '?format=full'
    EXEC sp_OAMethod @url, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', @sTmp0, @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN

        PRINT 'Gmail did not return an HTTP response.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @url
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp1 OUT
    IF (@success <> 1) or (@iTmp0 < 200) or (@iTmp1 >= 300)
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'Gmail HTTP ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @reply, 'Load', @success OUT, @sTmp0
        IF @success = 1
          BEGIN
            EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'error.message'
            PRINT @sTmp0
          END
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @url
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @reply, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Expected a Gmail JSON response.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @url
        RETURN
      END
    EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'payload'
    IF @iTmp0 <> 3
      BEGIN

        PRINT 'Expected a MIME payload.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @url
        RETURN
      END
    --  Queue paths into the original JSON document so any MIME nesting depth is supported.
    DECLARE @paths int
    EXEC @hr = sp_OACreate 'Chilkat.StringTable', @paths OUT

    EXEC sp_OAMethod @paths, 'Append', @success OUT, 'payload'
    IF @success <> 1
      BEGIN

        PRINT 'Could not queue the root part.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @reply
        EXEC @hr = sp_OADestroy @url
        EXEC @hr = sp_OADestroy @paths
        RETURN
      END
    DECLARE @part int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @part OUT

    DECLARE @childPath int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @childPath OUT

    DECLARE @p int
    SELECT @p = 0
    EXEC sp_OAGetProperty @paths, 'Count', @iTmp0 OUT
    WHILE (@p < @iTmp0)
      BEGIN
        EXEC sp_OAMethod @paths, 'StringAt', @sTmp0 OUT, @p
        EXEC sp_OAMethod @reply, 'ObjectOf2', @success OUT, @sTmp0, @part
        IF @success <> 1
          BEGIN

            PRINT 'Could not read a MIME part.'
            EXEC @hr = sp_OADestroy @token
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @reply
            EXEC @hr = sp_OADestroy @url
            EXEC @hr = sp_OADestroy @paths
            EXEC @hr = sp_OADestroy @part
            EXEC @hr = sp_OADestroy @childPath
            RETURN
          END

        EXEC sp_OAMethod @part, 'StringOf', @sTmp0 OUT, 'mimeType'

        EXEC sp_OAMethod @part, 'IntOf', @iTmp0 OUT, 'body.size'
        PRINT 'Part: ' + @sTmp0 + '  bytes: ' + @iTmp0
        EXEC sp_OAMethod @part, 'JsonTypeOf', @iTmp0 OUT, 'filename'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @part, 'StringOfEquals', @iTmp0 OUT, 'filename', '', 1
            IF @iTmp0 <> 1
              BEGIN

                EXEC sp_OAMethod @part, 'StringOf', @sTmp0 OUT, 'filename'
                PRINT 'Filename: ' + @sTmp0
              END
          END
        EXEC sp_OAMethod @part, 'JsonTypeOf', @iTmp0 OUT, 'body.attachmentId'
        IF @iTmp0 = 1
          BEGIN

            EXEC sp_OAMethod @part, 'StringOf', @sTmp0 OUT, 'body.attachmentId'
            PRINT 'Attachment ID: ' + @sTmp0
          END
        ELSE
          BEGIN
            EXEC sp_OAMethod @part, 'JsonTypeOf', @iTmp0 OUT, 'body.data'
            IF @iTmp0 = 1
              BEGIN

                PRINT 'Body data is available in this part.'
              END
          END
        DECLARE @j int
        SELECT @j = 0
        EXEC sp_OAMethod @part, 'SizeOfArray', @iTmp0 OUT, 'parts'
        WHILE (@j < @iTmp0)
          BEGIN
            EXEC sp_OAMethod @paths, 'StringAt', @sTmp0 OUT, @p
            EXEC sp_OAMethod @childPath, 'SetString', @success OUT, @sTmp0
            EXEC sp_OAMethod @childPath, 'Append', @success OUT, '.parts['
            EXEC sp_OAMethod @childPath, 'AppendInt', @success OUT, @j
            EXEC sp_OAMethod @childPath, 'Append', @success OUT, ']'
            EXEC sp_OAMethod @childPath, 'GetAsString', @sTmp0 OUT
            EXEC sp_OAMethod @paths, 'Append', @success OUT, @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not queue a child part.'
                EXEC @hr = sp_OADestroy @token
                EXEC @hr = sp_OADestroy @http
                EXEC @hr = sp_OADestroy @resp
                EXEC @hr = sp_OADestroy @reply
                EXEC @hr = sp_OADestroy @url
                EXEC @hr = sp_OADestroy @paths
                EXEC @hr = sp_OADestroy @part
                EXEC @hr = sp_OADestroy @childPath
                RETURN
              END
            SELECT @j = @j + 1
          END
        SELECT @p = @p + 1
      END

    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @reply
    EXEC @hr = sp_OADestroy @url
    EXEC @hr = sp_OADestroy @paths
    EXEC @hr = sp_OADestroy @part
    EXEC @hr = sp_OADestroy @childPath


END
GO