Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) Send Encrypted Email to Multiple RecipientsDemonstrates 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.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). 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 -- Use "Chilkat_9_5_0.MailMan" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.Cert" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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 -- Use "Chilkat_9_5_0.Email" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.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-2024 Chilkat Software, Inc. All Rights Reserved.