Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

SQL Server Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(SQL Server) Send Encrypted Email to Multiple Recipients

Demonstrates how to create and send an S/MIME encrypted email to multiple recipients. The digital certificate of each recipient is required. The encrypting/sending process uses each recipient's digital certificate (which internally contains the public key). Each recipient decrypts the received email using his/her private key.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- The mailman object is used for sending and receiving email.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.MailMan', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- Set the SMTP server.
    EXEC sp_OASetProperty @mailman, 'SmtpHost', 'smtp.mymailserver.com'

    -- Load each recipient's certificate into a Chilkat certificate object.
    -- This example loads the certificates from files.  However, the Chilkat
    -- certificate object provides other means for loading certificates,
    -- such as from in-memory PEM strings, or in-memory binary DER encoded form, etc.
    DECLARE @cert1 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert1 OUT

    DECLARE @success int
    EXEC sp_OAMethod @cert1, 'LoadFromFile', @success OUT, 'recipient1.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert1, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        RETURN
      END
    DECLARE @cert2 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert2 OUT

    EXEC sp_OAMethod @cert2, 'LoadFromFile', @success OUT, 'recipient2.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        RETURN
      END
    DECLARE @cert3 int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Cert', @cert3 OUT

    EXEC sp_OAMethod @cert3, 'LoadFromFile', @success OUT, 'recipient3.cer'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @cert3, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        EXEC @hr = sp_OADestroy @cert3
        RETURN
      END

    -- Create a new email object
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'This email is encrypted and sent to 3 recipients'
    EXEC sp_OASetProperty @email, 'Body', 'This is an S/MIME encrypted mail sent to 3 recipients'
    EXEC sp_OASetProperty @email, 'From', 'Chilkat Support <support@chilkatsoft.com>'

    -- Make each of the certificates available for encrypting the email
    -- by calling AddEncryptCert for each.
    EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert1
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert2
      END
    IF @success = 1
      BEGIN
        EXEC sp_OAMethod @email, 'AddEncryptCert', @success OUT, @cert3
      END
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @cert1
        EXEC @hr = sp_OADestroy @cert2
        EXEC @hr = sp_OADestroy @cert3
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    -- Add 3 recipients to the email (2 TO addresses, and 1 CC address)
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient 1', 'admin@cknotes.com'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Recipient 2', 'somebody001122@yahoo.com'
    EXEC sp_OAMethod @email, 'AddCC', @success OUT, 'Recipient 3', 'somebody123xyz@gmail.com'

    -- Indicate that the email is to be sent encrypted.
    EXEC sp_OASetProperty @email, 'SendEncrypted', 1

    -- Send the encrypted email...
    EXEC sp_OAMethod @mailman, 'SendEmail', @success OUT, @email
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
      END
    ELSE
      BEGIN

        PRINT 'Encrypted Email Sent!'
      END

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @cert1
    EXEC @hr = sp_OADestroy @cert2
    EXEC @hr = sp_OADestroy @cert3
    EXEC @hr = sp_OADestroy @email


END
GO

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.