Sample code for 30+ languages & platforms
SQL Server

Get the Certificate an Email Was Encrypted By

See more Email Object Examples

Demonstrates the read-only Chilkat Email.EncryptedBy property. If the email was received encrypted, this property contains descriptive details of the certificate used for encryption. It is meaningful only when ReceivedEncrypted is true. To obtain a certificate object rather than descriptive text, use GetEncryptedByCert or LastDecryptCert. This example loads a received email and prints the encryption certificate details.

Background: In S/MIME encryption, the sender encrypts the message using the recipient's public-key certificate — a signed document that binds an identity (name and email) to a public key. EncryptedBy reports which certificate was used, letting the recipient confirm the message was encrypted specifically for them. To work with the certificate programmatically (inspecting its subject, issuer, expiration, etc.), request the Cert object via GetEncryptedByCert.

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.EncryptedBy property.
    --  If the email was received encrypted, EncryptedBy contains the details of
    --  the certificate used for encryption.  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

    EXEC sp_OAMethod @email, 'LoadEml', @success OUT, 'qa_data/eml/encrypted.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    EXEC sp_OAGetProperty @email, 'ReceivedEncrypted', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN

        EXEC sp_OAGetProperty @email, 'EncryptedBy', @sTmp0 OUT
        PRINT 'Encrypted by: ' + @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'This email was not received encrypted.'
      END

    EXEC @hr = sp_OADestroy @email


END
GO