![]() |
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) Fetch 1st N Headers of Search ResultsCalls Search to get a message set, then downloads the 1st N messages in the message set. There are two equivalent ways of doing it: (1) iterate from 0 to N-1 and download each message individually, or (2) create a new message set that contains the 1st N messages and pass it to FetchHeaders. Both are demonstrated here. Note: This example requires Chilkat v11.0.0 or greater.
-- 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 assumes 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 -- Login EXEC sp_OAMethod @imap, 'Login', @success OUT, '****', '****' 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 -- Get the message IDs of all the emails in the mailbox -- We can choose to fetch UIDs or sequence numbers. DECLARE @fetchUids int SELECT @fetchUids = 1 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 @numFound int EXEC sp_OAGetProperty @messageSet, 'Count', @numFound OUT IF @numFound = 0 BEGIN PRINT 'No messages found.' EXEC @hr = sp_OADestroy @imap EXEC @hr = sp_OADestroy @messageSet RETURN END -- Get the 1st 10 messages in messageSet. DECLARE @upperBound int SELECT @upperBound = 10 IF @numFound < @upperBound BEGIN SELECT @upperBound = @numFound END DECLARE @i int SELECT @i = 0 DECLARE @bUid int EXEC sp_OAGetProperty @messageSet, 'HasUids', @bUid OUT DECLARE @headerOnly int SELECT @headerOnly = 1 DECLARE @email int EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT WHILE @i < @upperBound BEGIN EXEC sp_OAMethod @messageSet, 'GetId', @iTmp0 OUT, @i EXEC sp_OAMethod @imap, 'FetchEmail', @success OUT, @headerOnly, @iTmp0, @bUid, @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 RETURN END EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT PRINT @i + ': ' + @sTmp0 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 END GO |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.