Sample code for 30+ languages & platforms
SQL Server

Amazon SES v2: Verify a sending domain with Easy DKIM

See more Amazon SES Examples

Use a domain you control. Run once to create the regional identity, publish the returned CNAMEs with your DNS provider, then set checkExisting to true and run again to read the status. The target suffix comes from SES. If your DNS provider appends the domain automatically, enter just the relative record name.

CreateEmailIdentity · GetEmailIdentity · Easy DKIM records

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 @success int
    SELECT @success = 0

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

    DECLARE @domainName nvarchar(4000)
    SELECT @domainName = 'example.com'
    DECLARE @checkExisting int
    SELECT @checkExisting = 0
    --  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
    DECLARE @responseText nvarchar(4000)
    SELECT @responseText = ''
    IF @checkExisting = 1
      BEGIN
        DECLARE @path int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @path OUT

        EXEC sp_OAMethod @path, 'Append', @success OUT, '/v2/email/identities/'
        EXEC sp_OAMethod @path, 'Append', @success OUT, @domainName
        EXEC sp_OAMethod @path, 'GetAsString', @sTmp0 OUT
        EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', @sTmp0
      END
    ELSE
      BEGIN
        DECLARE @identity int
        EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @identity OUT

        EXEC sp_OAMethod @identity, 'UpdateString', @success OUT, 'EmailIdentity', @domainName
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/json'
        EXEC sp_OAMethod @identity, 'Emit', @sTmp0 OUT
        EXEC sp_OAMethod @rest, 'FullRequestString', @responseText OUT, 'POST', '/v2/email/identities', @sTmp0
      END
    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 @identity
        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 @identity
        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 @identity
        EXEC @hr = sp_OADestroy @response
        RETURN
      END


    EXEC sp_OAMethod @response, 'StringOf', @sTmp0 OUT, 'DkimAttributes.Status'
    PRINT 'DKIM status: ' + @sTmp0

    EXEC sp_OAMethod @response, 'BoolOf', @iTmp0 OUT, 'VerifiedForSendingStatus'
    PRINT 'Verified for sending: ' + @iTmp0
    DECLARE @count int
    EXEC sp_OAMethod @response, 'SizeOfArray', @count OUT, 'DkimAttributes.Tokens'
    IF @count < 1
      BEGIN

        PRINT 'No Easy DKIM tokens were returned. Check this identity''s DKIM configuration.'
        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 @identity
        EXEC @hr = sp_OADestroy @response
        RETURN
      END
    EXEC sp_OAMethod @response, 'HasMember', @iTmp0 OUT, 'DkimAttributes.SigningHostedZone'
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Find the CNAME targets for this identity in the SES console.'
        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 @identity
        EXEC @hr = sp_OADestroy @response
        RETURN
      END
    DECLARE @zone nvarchar(4000)
    EXEC sp_OAMethod @response, 'StringOf', @zone OUT, 'DkimAttributes.SigningHostedZone'
    DECLARE @i int
    SELECT @i = 0
    WHILE (@i < @count)
      BEGIN
        EXEC sp_OASetProperty @response, 'I', @i
        DECLARE @token nvarchar(4000)
        EXEC sp_OAMethod @response, 'StringOf', @token OUT, 'DkimAttributes.Tokens[i]'




        PRINT @token + '._domainkey.' + @domainName + ' CNAME ' + @token + '.' + @zone
        SELECT @i = @i + 1
      END

    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 @identity
    EXEC @hr = sp_OADestroy @response


END
GO