Sample code for 30+ languages & platforms
SQL Server

Check if a Received Email Was Decrypted

See more Email Object Examples

Demonstrates the read-only Chilkat Email.Decrypted property, which is true if the email arrived encrypted and was successfully decrypted. This property is only meaningful when ReceivedEncrypted is true, so you should always check ReceivedEncrypted first — a false value can mean either that the message was not encrypted or that decryption did not succeed. This example loads a received email and reports its encryption/decryption status.

Background: Encrypted email typically uses S/MIME. The sender encrypts the message with the recipient's public-key certificate, and only the holder of the matching private key can decrypt it. When Chilkat loads such a message and has access to the private key, it decrypts automatically; ReceivedEncrypted tells you the message arrived encrypted, and Decrypted tells you whether decryption succeeded.

Chilkat SQL Server Downloads

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

    --  Demonstrates the Email.Decrypted property.
    --  Decrypted is true if the email arrived encrypted and was successfully
    --  decrypted.  It is only meaningful when ReceivedEncrypted is true.

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

    --  Load an email that was received (from a .eml file).
    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/received.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Always check ReceivedEncrypted first.
    EXEC sp_OAGetProperty @email, 'ReceivedEncrypted', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'Decrypted', @iTmp0 OUT
        IF @iTmp0 = 1
          BEGIN

            PRINT 'The email was encrypted and was successfully decrypted.'
          END
        ELSE
          BEGIN

            PRINT 'The email was encrypted but could NOT be decrypted.'
          END
      END
    ELSE
      BEGIN

        PRINT 'The email did not arrive encrypted.'
      END

    EXEC @hr = sp_OADestroy @email


END
GO