Sample code for 30+ languages & platforms
SQL Server

Amazon SES v2: Publish delivery events to SNS and correlate a tagged send

See more Amazon SES Examples

Use an existing configuration set and Standard SNS topic in the same Region. Grant SES permission to publish to the topic and subscribe your consumer. Configure the destination once, then set configureDestination to false to send a test. In the SES event inside the SNS Message, eventType identifies the outcome, mail.messageId matches the send result, and mail.tags.notification_ref[0] identifies the application record. Use the SES bounce or complaint simulator address to exercise those outcomes.

Configure the SNS destination · SES event payload

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @sTmp1 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0

    DECLARE @configureDestination int
    SELECT @configureDestination = 1
    DECLARE @setName nvarchar(4000)
    SELECT @setName = 'application-mail'
    --  Use the same AWS Region for the identity, endpoint, and Signature Version 4.
    DECLARE @region nvarchar(4000)
    SELECT @region = 'us-west-2'
    DECLARE @host int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @host OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @host, 'Append', @success OUT, 'email.'
    EXEC sp_OAMethod @host, 'Append', @success OUT, @region
    EXEC sp_OAMethod @host, 'Append', @success OUT, '.amazonaws.com'

    DECLARE @signer int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @signer OUT

    --  Supply the credentials from your application's credential provider.
    EXEC sp_OASetProperty @signer, 'AccessKey', 'AWS_ACCESS_KEY_ID'
    EXEC sp_OASetProperty @signer, 'SecretKey', 'AWS_SECRET_ACCESS_KEY'
    EXEC sp_OASetProperty @signer, 'Region', @region
    EXEC sp_OASetProperty @signer, 'ServiceName', 'ses'

    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OAMethod @host, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 10000
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @connection, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END

    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @signer
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    --  For temporary AWS credentials, supply the session token from the same provider.
    DECLARE @sessionToken nvarchar(4000)
    SELECT @sessionToken = ''
    IF @sessionToken <> ''
      BEGIN
        EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sessionToken
      END
    IF @configureDestination = 1
      BEGIN
        DECLARE @path int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @path OUT

        EXEC sp_OAMethod @path, 'Append', @success OUT, '/v2/email/configuration-sets/'
        EXEC sp_OAMethod @path, 'Append', @success OUT, @setName
        EXEC sp_OAMethod @path, 'Append', @success OUT, '/event-destinations'
        DECLARE @destination int
        EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @destination OUT

        EXEC sp_OAMethod @destination, 'UpdateString', @success OUT, 'EventDestinationName', 'delivery-to-sns'
        EXEC sp_OAMethod @destination, 'UpdateString', @success OUT, 'EventDestination.MatchingEventTypes[0]', 'DELIVERY'
        EXEC sp_OAMethod @destination, 'UpdateString', @success OUT, 'EventDestination.MatchingEventTypes[1]', 'BOUNCE'
        EXEC sp_OAMethod @destination, 'UpdateString', @success OUT, 'EventDestination.MatchingEventTypes[2]', 'COMPLAINT'
        EXEC sp_OAMethod @destination, 'UpdateString', @success OUT, 'EventDestination.SnsDestination.TopicArn', 'arn:aws:sns:us-west-2:123456789012:ses-delivery'
        EXEC sp_OAMethod @destination, 'UpdateBool', @success OUT, 'EventDestination.Enabled', 1

        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
        DECLARE @setupResponse nvarchar(4000)
        EXEC sp_OAMethod @path, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @destination, 'Emit', @sTmp1 OUT
        EXEC sp_OAMethod @rest, 'FullRequestString', @setupResponse OUT, 'POST', @sTmp0, @sTmp1
        EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 <> 1
          BEGIN
            EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @host
            EXEC @hr = sp_OADestroy @signer
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @path
            EXEC @hr = sp_OADestroy @destination
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT

            PRINT 'SES returned HTTP ' + @iTmp0 + ' ' + @setupResponse
            EXEC @hr = sp_OADestroy @host
            EXEC @hr = sp_OADestroy @signer
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @path
            EXEC @hr = sp_OADestroy @destination
            RETURN
          END

        PRINT 'Destination created. Use the send branch to test it.'
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @path
        EXEC @hr = sp_OADestroy @destination
        RETURN
      END
    DECLARE @request int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @request OUT

    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'FromEmailAddress', 'sender@example.com'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'Destination.ToAddresses[0]', 'success@simulator.amazonses.com'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'Content.Simple.Subject.Data', 'Delivery event test'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'Content.Simple.Body.Text.Data', 'Exercise the application-mail configuration set.'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'EmailTags[0].Name', 'notification_ref'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'EmailTags[0].Value', 'notify-1042'
    EXEC sp_OAMethod @request, 'UpdateString', @success OUT, 'ConfigurationSetName', @setName

    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @request, 'Emit', @sTmp0 OUT
    EXEC sp_OAMethod @rest, 'FullRequestString', @responseText OUT, 'POST', '/v2/email/outbound-emails', @sTmp0
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @path
        EXEC @hr = sp_OADestroy @destination
        EXEC @hr = sp_OADestroy @request
        RETURN
      END
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT

        PRINT 'SES returned HTTP ' + @iTmp0 + ' ' + @responseText
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @path
        EXEC @hr = sp_OADestroy @destination
        EXEC @hr = sp_OADestroy @request
        RETURN
      END
    DECLARE @response int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @response OUT

    EXEC sp_OAMethod @response, 'Load', @success OUT, @responseText
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @response, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @host
        EXEC @hr = sp_OADestroy @signer
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @path
        EXEC @hr = sp_OADestroy @destination
        EXEC @hr = sp_OADestroy @request
        EXEC @hr = sp_OADestroy @response
        RETURN
      END

    EXEC sp_OAMethod @response, 'StringOf', @sTmp0 OUT, 'MessageId'
    PRINT 'SES accepted the message: ' + @sTmp0

    EXEC @hr = sp_OADestroy @host
    EXEC @hr = sp_OADestroy @signer
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @path
    EXEC @hr = sp_OADestroy @destination
    EXEC @hr = sp_OADestroy @request
    EXEC @hr = sp_OADestroy @response


END
GO