SQL Server GMail: Search for Messages with Subject Containing a Word

Back to Index

Return messages matching the specified query. See https://support.google.com/mail/answer/7190?hl=en for additional information about search operators. This example searches for all emails having the whole word "ADVChina" in the subject.

Documentation: https://support.google.com/mail/answer/7190?hl=en

CURL Command

curl -X GET https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:ADVChina \
    --header "Authorization: Bearer GMAIL_TOKEN"

SQL Server Example

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    --  URL: https://www.googleapis.com/gmail/v1/users/me/messages?q=subject:ADVChina
    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @port int
    SELECT @port = 443
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'www.googleapis.com', @port, @bTls, @bAutoReconnect
    IF @success <> 1
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ConnectFailReason', @iTmp0 OUT
        PRINT 'ConnectFailReason: ' + @iTmp0
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', 'Bearer GMAIL_TOKEN'

    DECLARE @sbResponseBody int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @rest, 'FullRequestNoBodySb', @success OUT, 'GET', '/gmail/v1/users/me/messages?q=subject:ADVChina', @sbResponseBody
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END
    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @respStatusCode OUT
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Status Code = ' + @respStatusCode

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'
        EXEC sp_OAMethod @sbResponseBody, 'GetAsString', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @sbResponseBody
        RETURN
      END

    DECLARE @jsonResponse int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @jsonResponse OUT

    EXEC sp_OAMethod @jsonResponse, 'LoadSb', @success OUT, @sbResponseBody

    --  See the Online Tool for Generating JSON Parse Code
    DECLARE @i int

    DECLARE @count_i int

    DECLARE @resultSizeEstimate int
    EXEC sp_OAMethod @jsonResponse, 'IntOf', @resultSizeEstimate OUT, 'resultSizeEstimate'
    SELECT @i = 0
    EXEC sp_OAMethod @jsonResponse, 'SizeOfArray', @count_i OUT, 'messages'
    WHILE @i < @count_i
      BEGIN
        EXEC sp_OASetProperty @jsonResponse, 'I', @i
        DECLARE @id nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @id OUT, 'messages[i].id'
        DECLARE @threadId nvarchar(4000)
        EXEC sp_OAMethod @jsonResponse, 'StringOf', @threadId OUT, 'messages[i].threadId'
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jsonResponse


END
GO

Sample JSON Response Body

{
  "messages": [
    {
      "id": "166e50fed0b9b0cb",
      "threadId": "166e50fed0b9b0cb"
    },
    {
      "id": "166c12e5fee013fe",
      "threadId": "166c12e5fee013fe"
    },
    {
      "id": "1669cc9a926bb8c1",
      "threadId": "1669cc9a926bb8c1"
    },
    {
      "id": "16678c485e7f0a0c",
      "threadId": "16678c485e7f0a0c"
    }
  ],
  "resultSizeEstimate": 4
}