![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) S/MIME Encrypt .eml without SendingSee more Email Object ExamplesDemonstrates how to encrypt an email using the recipient's digital certificate. This example just encrypts, and does not send the email.
-- 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) DECLARE @success int SELECT @success = 0 -- This example requires the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. 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, 'c:/temp/email/unencrypted.eml' IF @success = 0 BEGIN EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @email RETURN END -- The email content is encrypted using AES with a 256-bit key, operating in GCM mode, which provides authenticated encryption. EXEC sp_OASetProperty @email, 'Pkcs7CryptAlg', 'aes-gcm' EXEC sp_OASetProperty @email, 'Pkcs7KeyLength', 256 EXEC sp_OASetProperty @email, 'OaepPadding', 1 EXEC sp_OASetProperty @email, 'OaepHash', 'sha256' EXEC sp_OASetProperty @email, 'OaepMgfHash', 'sha256' DECLARE @cert int EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT EXEC sp_OAMethod @cert, 'LoadFromFile', @success OUT, 'c/temps/cert/recipient.cer' IF @success = 0 BEGIN EXEC sp_OAGetProperty @cert, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @email EXEC @hr = sp_OADestroy @cert RETURN END EXEC sp_OASetProperty @email, 'SendEncrypted', 1 EXEC sp_OAMethod @email, 'SetEncryptCert', @success OUT, @cert DECLARE @sbSmime int EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbSmime OUT -- The mailman object applies the encryption by rendering the email according to the instructions (property settings) provided in the email object. -- No email is sent. DECLARE @mailman int EXEC @hr = sp_OACreate 'Chilkat.MailMan', @mailman OUT EXEC sp_OAMethod @mailman, 'RenderToMimeSb', @success OUT, @email, @sbSmime IF @success = 0 BEGIN EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @email EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbSmime EXEC @hr = sp_OADestroy @mailman RETURN END EXEC sp_OAMethod @sbSmime, 'WriteFile', @success OUT, 'c:/temp/encryptedEmail.eml', 'utf-8', 0 IF @success = 0 BEGIN EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @email EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbSmime EXEC @hr = sp_OADestroy @mailman RETURN END PRINT 'Success!' EXEC @hr = sp_OADestroy @email EXEC @hr = sp_OADestroy @cert EXEC @hr = sp_OADestroy @sbSmime EXEC @hr = sp_OADestroy @mailman END GO |
||||
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.