SQL Server
SQL Server
Add an S/MIME Detached Signature
See more MIME Examples
Demonstrates the Chilkat Mime.AddDetachedSignature method, which creates an S/MIME detached signature. The only argument is the signing Cert, which must have access to its private key. On success the entity becomes a multipart/signed with the content and a separate signature part.
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: A detached signature (
multipart/signed) keeps the original content readable alongside a separate signature part — so recipients whose software does not understand S/MIME still see the message, they just cannot verify it. That backward compatibility makes detached the usual choice for signed email. The signer proves authorship with a certificate that carries its private key.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
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
-- Create an S/MIME detached signature. The object becomes a multipart/signed entity with the
-- original content and a separate signature part.
EXEC sp_OAMethod @mime, 'AddDetachedSignature', @success OUT, @cert
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.'
EXEC @hr = sp_OADestroy @mime
EXEC @hr = sp_OADestroy @cert
END
GO