SQL Server Requires Chilkat v11.0.0+
SQL Server
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.
Chilkat SQL Server Downloads
--
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 @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
-- Use the authorized mailbox or one of its configured send-as addresses.
EXEC sp_OASetProperty @email, 'From', 'Sender <sender@example.com>'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient', 'recipient@example.net'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @email
RETURN
END
EXEC sp_OASetProperty @email, 'Subject', 'Sample message'
EXEC sp_OASetProperty @email, 'Charset', 'utf-8'
EXEC sp_OASetProperty @email, 'Body', 'Hello from the Gmail API.'
DECLARE @fileData int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @fileData OUT
EXEC sp_OAMethod @fileData, 'LoadFile', @success OUT, 'attachment.pdf'
IF @success <> 1
BEGIN
PRINT 'Could not read attachment.pdf.'
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @fileData
RETURN
END
EXEC sp_OAMethod @email, 'AddAttachmentBd', @success OUT, 'attachment.pdf', @fileData, 'application/pdf'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @fileData
RETURN
END
DECLARE @mime int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @mime OUT
EXEC sp_OAMethod @email, 'GetMimeBd', @success OUT, @mime
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
RETURN
END
EXEC sp_OAMethod @http, 'HttpBd', @success OUT, 'POST', 'https://gmail.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media', @mime, 'message/rfc822', @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 @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
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 @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
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 @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
RETURN
END
EXEC sp_OAMethod @reply, 'JsonTypeOf', @iTmp0 OUT, 'id'
EXEC sp_OAMethod @reply, 'StringOfEquals', @iTmp1 OUT, 'id', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected id in the 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 @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
RETURN
END
EXEC sp_OAMethod @reply, 'StringOf', @sTmp0 OUT, 'id'
PRINT 'Gmail message ID: ' + @sTmp0
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @reply
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @fileData
EXEC @hr = sp_OADestroy @mime
END
GO