Sample code for 30+ languages & platforms
SQL Server

Add a Detached Signature (Header Placement Control)

See more MIME Examples

Demonstrates the Chilkat Mime.AddDetachedSignature2 method, which creates the same multipart/signed structure as AddDetachedSignature, with a second argument controlling whether the existing message header fields are transferred to the new outer entity.

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: Signing wraps the content in a new outer multipart/signed, which raises the question of where the message-level headers (Subject, From, To) should live. This variant lets you choose: transfer them up to the outer entity so the signed message looks like a normal email, or leave them on the inner part. Transferring is typical for email; the finer control matters when the signed entity is embedded in a larger structure.

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 @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    EXEC sp_OAMethod @mime, 'SetBodyFromPlainText', @success OUT, 'This message will be signed.'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END
    --  Load the signing certificate (which must have access to its private key) from a PFX file.
    --  The PFX password should come from a secure source rather than being hard-coded.
    DECLARE @pfxPassword nvarchar(4000)
    SELECT @pfxPassword = 'myPfxPassword'
    DECLARE @cert int
    EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT

    EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'qa_data/signer.pfx', @pfxPassword
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    --  AddDetachedSignature2 is the same as AddDetachedSignature, but the 2nd argument controls whether
    --  the existing message header fields are transferred to the new outer multipart/signed entity.
    DECLARE @transferHeaderFields int
    SELECT @transferHeaderFields = 1
    EXEC sp_OAMethod @mime, 'AddDetachedSignature2', @success OUT, @cert, @transferHeaderFields
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    EXEC sp_OAMethod @mime, 'SaveMime', @success OUT, 'qa_output/signed.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @cert
        RETURN
      END

    PRINT 'Detached signature added (headers transferred).'

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @cert


END
GO