SQL Server
SQL Server
IMAP Download and Verify Signed (S/MIME) Email
See more IMAP Examples
Demonstrates how to download and verify digitally signed S/MIME email.Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 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 @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect to an IMAP server.
-- Use TLS
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.example.com'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
EXEC sp_OAMethod @imap, 'Login', @success OUT, 'myLogin', 'myPassword'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Select an IMAP mailbox
EXEC sp_OAMethod @imap, 'SelectMailbox', @success OUT, 'Inbox'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- We can choose to fetch UIDs or sequence numbers.
DECLARE @fetchUids int
SELECT @fetchUids = 1
-- Get the message IDs of all the emails in the mailbox
DECLARE @messageSet int
EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @messageSet OUT
EXEC sp_OAMethod @imap, 'QueryMbx', @success OUT, 'ALL', @fetchUids, @messageSet
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
RETURN
END
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
DECLARE @cert int
EXEC @hr = sp_OACreate 'Chilkat.Cert', @cert OUT
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @messageSet, 'Count', @iTmp0 OUT
WHILE @i < @iTmp0
BEGIN
DECLARE @uid nvarchar(4000)
EXEC sp_OAMethod @messageSet, 'GetId', @uid OUT, @i
PRINT 'uid: ' + @uid
EXEC sp_OAMethod @imap, 'FetchEmail', @success OUT, 0, @uid, 1, @email
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
RETURN
END
-- The security layers of signed and/or encrypted emails
-- are automatically "unwrapped" when loaded into
-- a Chilkat email object.
-- An application only needs to check to see if an email
-- was received signed or encrypted, and then examine
-- the success/failure. For example:
EXEC sp_OAGetProperty @email, 'ReceivedSigned', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'This email was signed.'
-- Check to see if the signatures were verified.
EXEC sp_OAGetProperty @email, 'SignaturesValid', @iTmp0 OUT
IF @iTmp0 = 1
BEGIN
PRINT 'Digital signature(s) verified.'
EXEC sp_OAGetProperty @email, 'SignedBy', @sTmp0 OUT
PRINT 'Signer: ' + @sTmp0
-- Get the certificate used for signing.
EXEC sp_OAMethod @email, 'LastSignerCert', @success OUT, 0, @cert
IF @success = 0
BEGIN
PRINT 'Failed to get signing certificate object.'
END
ELSE
BEGIN
EXEC sp_OAGetProperty @cert, 'SubjectCN', @sTmp0 OUT
PRINT 'Signing cert: ' + @sTmp0
END
END
ELSE
BEGIN
PRINT 'Digital signature verification failed.'
END
END
SELECT @i = @i + 1
END
-- Disconnect from the IMAP server.
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @cert
END
GO