Sample code for 30+ languages & platforms
SQL Server

Remove All S/MIME Security Layers (UnwrapSecurity)

See more MIME Examples

Demonstrates the Chilkat Mime.UnwrapSecurity method, which removes supported S/MIME security layers and restores the underlying MIME content. It takes no arguments and handles both layer orders: signed-then-encrypted and encrypted-then-signed.

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 message can be both signed and encrypted, in either order, producing nested S/MIME wrappers. UnwrapSecurity peels them all off in one call — verifying signatures and decrypting as it goes — so you do not have to detect the layering and call Verify and Decrypt in the right sequence yourself. Decryption still requires the recipient's certificate and private key to be configured.

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 Mime.UnwrapSecurity method, which removes supported S/MIME security layers and
    --  restores the underlying MIME content.  It takes no arguments and handles both layer orders
    --  (signed-then-encrypted and encrypted-then-signed).

    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, 'LoadMimeFile', @success OUT, 'qa_data/secured.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  A single call peels off the signature and/or encryption layers.  (Decryption requires the
    --  appropriate certificate/private key to be configured first.)
    EXEC sp_OAMethod @mime, 'UnwrapSecurity', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END


    EXEC sp_OAGetProperty @mime, 'ContentType', @sTmp0 OUT
    PRINT 'Security layers removed. Content-Type is now: ' + @sTmp0

    EXEC @hr = sp_OADestroy @mime


END
GO