Sample code for 30+ languages & platforms
SQL Server

Set the DKIM Signing Private Key

See more DKIM / DomainKey Examples

Demonstrates the Chilkat Dkim.SetDkimPrivateKey method, which supplies the RSA private key used by DkimSign. The only argument is a PrivateKey object. The source key may be destroyed after this returns, and the configured key can be reused for multiple signatures.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: DKIM proves a message really came from its domain by signing it with a private key that only the domain owner holds; recipients verify with the matching public key published in the domain's DNS. This method loads that private half into the Dkim object once, so a mail server can sign every outgoing message without re-reading the key each time. Because the key is the domain's signing secret, it should be protected like any other private key and never committed to source.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Dkim.SetDkimPrivateKey method, which supplies the RSA private key used by
    --  DkimSign.  The only argument is a PrivateKey object.

    DECLARE @dkim int
    EXEC @hr = sp_OACreate 'Chilkat.Dkim', @dkim OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Load the RSA private key that corresponds to the public key published in the domain's DNS.
    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT

    EXEC sp_OAMethod @privKey, 'LoadPemFile', @success OUT, 'qa_data/dkim_private.pem'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @dkim
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    --  Copy the key into the Dkim object.  The PrivateKey object may be destroyed afterward, and the
    --  configured key can be reused for multiple signatures.
    EXEC sp_OAMethod @dkim, 'SetDkimPrivateKey', @success OUT, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @dkim, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @dkim
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    PRINT 'DKIM private key configured.'

    EXEC @hr = sp_OADestroy @dkim
    EXEC @hr = sp_OADestroy @privKey


END
GO