SQL Server Requires Chilkat v11.0.0+
SQL Server
Sign an email and send the signed bytes over SMTP
See more DKIM Examples
Compose the email, render it to MIME once, add its DKIM signature, and submit those same bytes to an SMTP server. Put blind-copy recipients in the SMTP envelope only so their addresses stay out of the visible headers.
Use an authorized SMTP account and your own sender domain. Publish the selector’s matching public key first. If your relay rewrites signed content, have it apply the signature after its final edits. A successful SMTP submission means the relay accepted the message. A recipient can then verify it with the public key in DNS.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
DECLARE @mail int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mail OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @mail, 'SmtpHost', 'smtp.example.com'
EXEC sp_OASetProperty @mail, 'SmtpPort', 465
EXEC sp_OASetProperty @mail, 'SmtpSsl', 1
EXEC sp_OASetProperty @mail, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @mail, 'SmtpUsername', 'SMTP_USERNAME'
EXEC sp_OASetProperty @mail, 'SmtpPassword', 'SMTP_PASSWORD'
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
EXEC sp_OASetProperty @email, 'From', 'Workshop team <updates@example.com>'
EXEC sp_OASetProperty @email, 'Subject', 'Workshop registration'
EXEC sp_OASetProperty @email, 'Body', 'Registration opens on Monday.'
EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Newsletter editor', 'editor@example.net'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
RETURN
END
-- Render after all headers, recipients, and content are final.
DECLARE @message int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @message OUT
EXEC sp_OAMethod @mail, 'RenderToMimeBd', @success OUT, @email, @message
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mail, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
RETURN
END
-- Supply your RSA signing key. For an encrypted PEM, get its password from your credential store.
DECLARE @privateKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privateKey OUT
EXEC sp_OAMethod @privateKey, 'LoadAnyFormatFile', @success OUT, 'dkim-private.pem', ''
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @privateKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @privateKey
RETURN
END
DECLARE @signer int
EXEC @hr = sp_OACreate 'Chilkat.Dkim', @signer OUT
EXEC sp_OASetProperty @signer, 'DkimDomain', 'example.com'
EXEC sp_OASetProperty @signer, 'DkimSelector', 'mail2026'
EXEC sp_OASetProperty @signer, 'DkimAlg', 'rsa-sha256'
EXEC sp_OAMethod @signer, 'SetDkimPrivateKey', @success OUT, @privateKey
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @signer, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @privateKey
EXEC @hr = sp_OADestroy @signer
RETURN
END
EXEC sp_OAMethod @signer, 'DkimSign', @success OUT, @message
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @signer, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @privateKey
EXEC @hr = sp_OADestroy @signer
RETURN
END
-- archive@example.com is an envelope-only blind copy.
DECLARE @recipients nvarchar(4000)
SELECT @recipients = 'editor@example.net,archive@example.com'
EXEC sp_OAMethod @mail, 'SendMimeBd', @success OUT, 'updates@example.com', @recipients, @message
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mail, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @privateKey
EXEC @hr = sp_OADestroy @signer
RETURN
END
PRINT 'SMTP relay accepted the signed message.'
EXEC sp_OAMethod @mail, 'CloseSmtpConnection', @success OUT
IF @success <> 1
BEGIN
PRINT 'The SMTP session did not close cleanly.'
END
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @message
EXEC @hr = sp_OADestroy @privateKey
EXEC @hr = sp_OADestroy @signer
END
GO