Sample code for 30+ languages & platforms
SQL Server

Fetch a Chunk of Messages, Tracking Failures

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchChunk2 method, which downloads a run of messages by sequence number and reports which succeeded and which failed. The arguments are the starting sequence number, the count, a MessageSet that receives the failed IDs, a MessageSet that receives the successfully-fetched IDs, and the EmailBundle. This example fetches the first chunk of messages and reports the success and failure counts.

Background: When bulk-downloading a large mailbox, individual messages can occasionally fail to fetch (a corrupt message on the server, a transient error) without failing the whole operation. FetchChunk2 is designed for that reality: it returns a partial EmailBundle plus two MessageSet objects so your code can log or retry exactly the messages that did not come through, rather than aborting the entire sync.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the Imap.FetchChunk2 method, which downloads a run of messages by sequence
    --  number and reports which succeeded and which failed.  The 1st argument is the starting
    --  sequence number, the 2nd is the count, the 3rd receives the failed IDs, the 4th receives
    --  the successfully-fetched IDs, and the 5th is the EmailBundle.

    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

    DECLARE @numMessages int
    EXEC sp_OAMethod @imap, 'SelectMailbox', @numMessages OUT, 'Inbox'
    IF @numMessages < 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    --  Fetch messages 1..25 (or fewer if the mailbox is smaller).
    DECLARE @count int
    SELECT @count = 25
    IF @count > @numMessages
      BEGIN
        SELECT @count = @numMessages
      END

    DECLARE @failedSet int
    EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @failedSet OUT

    DECLARE @fetchedSet int
    EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @fetchedSet OUT

    DECLARE @bundle int
    EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @bundle OUT

    EXEC sp_OAMethod @imap, 'FetchChunk2', @success OUT, 1, @count, @failedSet, @fetchedSet, @bundle
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @failedSet
        EXEC @hr = sp_OADestroy @fetchedSet
        EXEC @hr = sp_OADestroy @bundle
        RETURN
      END


    EXEC sp_OAGetProperty @fetchedSet, 'Count', @iTmp0 OUT
    PRINT 'Fetched: ' + @iTmp0

    EXEC sp_OAGetProperty @failedSet, 'Count', @iTmp0 OUT
    PRINT 'Failed:  ' + @iTmp0

    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 @failedSet
        EXEC @hr = sp_OADestroy @fetchedSet
        EXEC @hr = sp_OADestroy @bundle
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @failedSet
    EXEC @hr = sp_OADestroy @fetchedSet
    EXEC @hr = sp_OADestroy @bundle


END
GO