SQL Server Requires Chilkat v11.0.0+
SQL Server
Sign an EML using RSA-PSS and SHA-256
See more Email Object Examples
Load the intended signing certificate and its RSA private key from a PFX. Set the signature options on Email and render the final S/MIME message through MailMan. The resulting multipart/signed file can be verified independently. Supply the PFX password through your application’s credential store.
Chilkat SQL Server Downloads
--
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 @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'message.eml'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
RETURN
END
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
EXEC sp_OAMethod @cert, 'LoadPfxFile', @success OUT, 'signer.pfx', 'PFX_PASSWORD'
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
RETURN
END
EXEC sp_OAMethod @email, 'SetSigningCert', @success OUT, @cert
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
RETURN
END
EXEC sp_OASetProperty @email, 'SigningAlg', 'pss'
EXEC sp_OASetProperty @email, 'SigningHashAlg', 'sha256'
EXEC sp_OASetProperty @email, 'SendSigned', 1
DECLARE @mail int
EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mail OUT
EXEC sp_OASetProperty @mail, 'OpaqueSigning', 0
DECLARE @rendered int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @rendered OUT
EXEC sp_OAMethod @mail, 'RenderToMimeBd', @success OUT, @email, @rendered
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @mail, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @rendered
RETURN
END
EXEC sp_OAMethod @rendered, 'WriteFile', @success OUT, 'signed.eml'
IF @success <> 1
BEGIN
PRINT 'Failed: rendered.WriteFile.'
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @rendered
RETURN
END
PRINT 'Saved signed.eml.'
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
EXEC @hr = sp_OADestroy @mail
EXEC @hr = sp_OADestroy @rendered
END
GO