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) Subscribe to Mailboxes and List Subscribed MailboxesDemonstrates how to list subscribed mailboxes, and unsubscribe/subscribe to a mailbox.
-- 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) -- This example assumes the Chilkat API to have been previously unlocked. -- See Global Unlock Sample for sample code. DECLARE @imap int -- Use "Chilkat_9_5_0.Imap" for versions of Chilkat < 10.0.0 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 DECLARE @success int EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.someMailServer.com' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END -- Login EXEC sp_OAMethod @imap, 'Login', @success OUT, 'myLogin', 'myPassword' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END -- First examine the already subscribed mailboxes. DECLARE @refName nvarchar(4000) SELECT @refName = '' DECLARE @wildcardedMailbox nvarchar(4000) SELECT @wildcardedMailbox = '*' -- Alternatively, the ListMailboxes method can be called to list all mailboxes (both subscribed and unsubscribed). DECLARE @mboxes int EXEC sp_OAMethod @imap, 'ListSubscribed', @mboxes OUT, @refName, @wildcardedMailbox EXEC sp_OAGetProperty @imap, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END DECLARE @i int EXEC sp_OAGetProperty @mboxes, 'Count', @iTmp0 OUT SELECT @i = 0 WHILE @i <= @iTmp0 - 1 BEGIN EXEC sp_OAMethod @mboxes, 'GetName', @sTmp0 OUT, @i PRINT @sTmp0 SELECT @i = @i + 1 END PRINT '----' EXEC @hr = sp_OADestroy @mboxes -- Sample output. -- INBOX -- INBOX/recent -- INBOX/misc -- INBOX/misc/solutions -- ... -- Outbox -- Deleted Items -- Sent Items -- Drafts -- Junk E-mail -- Trash -- Sent -- Templates -- Let's unsubscribe to "INBOX/misc/solutions". EXEC sp_OAMethod @imap, 'Unsubscribe', @success OUT, 'INBOX/misc/solutions' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END -- Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is missing. EXEC sp_OAMethod @imap, 'ListSubscribed', @mboxes OUT, '', 'INBOX/misc*' EXEC sp_OAGetProperty @imap, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END EXEC sp_OAGetProperty @mboxes, 'Count', @iTmp0 OUT SELECT @i = 0 WHILE @i <= @iTmp0 - 1 BEGIN EXEC sp_OAMethod @mboxes, 'GetName', @sTmp0 OUT, @i PRINT @sTmp0 SELECT @i = @i + 1 END EXEC @hr = sp_OADestroy @mboxes PRINT '----' -- Re-subscribe to "INBOX/misc/solutions". EXEC sp_OAMethod @imap, 'Subscribe', @success OUT, 'INBOX/misc/solutions' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END -- Get the list of subscribed mailboxes again to verify that "INBOX/misc/solutions" is back in the list. EXEC sp_OAMethod @imap, 'ListSubscribed', @mboxes OUT, '', 'INBOX/misc*' EXEC sp_OAGetProperty @imap, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @imap RETURN END EXEC sp_OAGetProperty @mboxes, 'Count', @iTmp0 OUT SELECT @i = 0 WHILE @i <= @iTmp0 - 1 BEGIN EXEC sp_OAMethod @mboxes, 'GetName', @sTmp0 OUT, @i PRINT @sTmp0 SELECT @i = @i + 1 END EXEC @hr = sp_OADestroy @mboxes PRINT '----' -- Disconnect from the IMAP server. EXEC sp_OAMethod @imap, 'Disconnect', @success OUT EXEC @hr = sp_OADestroy @imap END GO |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.