SQL Server
SQL Server
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
-- MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
-- is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
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, 'user@example.com', 'myPassword'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
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
-- Find the messages to download (here, the unseen messages by UID).
DECLARE @msgSet int
EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @msgSet OUT
EXEC sp_OAMethod @imap, 'QueryMbx', @success OUT, 'UNSEEN', 1, @msgSet
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
RETURN
END
-- Download the messages in the set (full bodies) into a bundle.
DECLARE @bundle int
EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @bundle OUT
EXEC sp_OAMethod @imap, 'FetchMsgSet', @success OUT, 0, @msgSet, @bundle
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @bundle
RETURN
END
DECLARE @n int
EXEC sp_OAGetProperty @bundle, 'MessageCount', @n OUT
PRINT 'Fetched ' + @n + ' messages.'
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
DECLARE @i int
SELECT @i = 0
WHILE @i <= @n - 1
BEGIN
EXEC sp_OAMethod @bundle, 'EmailAt', @success OUT, @i, @email
EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT
PRINT @sTmp0
SELECT @i = @i + 1
END
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @bundle
EXEC @hr = sp_OADestroy @email
RETURN
END
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @bundle
EXEC @hr = sp_OADestroy @email
END
GO