Sample code for 30+ languages & platforms
SQL Server

List and Inspect IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Mailboxes class, a read-only result collection populated by Imap.MbxList. This example connects to an IMAP server, lists all mailboxes into a Mailboxes object, then loops from 0 to Count - 1 and inspects each mailbox: its name (GetName), whether it is selectable (IsSelectable) or marked (IsMarked), whether it may have child mailboxes (HasInferiors), a specific flag (HasFlag), and all of its flags both as a string (GetFlags) and individually (GetNumFlags with GetNthFlag). Finally it looks up a mailbox by name with GetMailboxIndex.

Background: Mailboxes is how an application renders an IMAP folder tree. Because it is read-only, you never construct it directly — you receive it from MbxList and read its entries by index. Mailbox names come back as normal Unicode text (Chilkat handles the modified UTF-7 / IMAP UTF-8 encoding at the protocol level), so a name from GetName can be passed straight to methods like SelectMailbox. Note that HasInferiors is often true: it means children exist or may be created, which is not the same as \HasChildren.

Chilkat SQL Server Downloads

SQL Server
-- 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
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates using Chilkat.Imap to populate a Mailboxes object (via MbxList), then looping
    --  over the collection to inspect each mailbox's name and attributes.  Mailboxes is a read-only
    --  result collection -- applications inspect the entries but do not add, remove, or modify them.

    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

    --  Populate a Mailboxes object with the full list of mailboxes.  The first argument requests
    --  all mailboxes (not subscribed-only) and "*" matches any name.
    DECLARE @subscribedOnly int
    SELECT @subscribedOnly = 0
    DECLARE @mailboxes int
    EXEC @hr = sp_OACreate 'Chilkat.Mailboxes', @mailboxes OUT

    EXEC sp_OAMethod @imap, 'MbxList', @success OUT, @subscribedOnly, '', '*', @mailboxes
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @mailboxes
        RETURN
      END

    --  Loop over each mailbox in the returned collection.
    DECLARE @n int
    EXEC sp_OAGetProperty @mailboxes, 'Count', @n OUT

    PRINT 'Number of mailboxes: ' + @n

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        DECLARE @name nvarchar(4000)
        EXEC sp_OAMethod @mailboxes, 'GetName', @name OUT, @i

        PRINT 'Mailbox: ' + @name

        --  Whether the mailbox can be selected or examined for message access.
        EXEC sp_OAMethod @mailboxes, 'IsSelectable', @iTmp0 OUT, @i
        IF @iTmp0
          BEGIN

            PRINT '  Selectable: yes'
          END
        ELSE
          BEGIN

            PRINT '  Selectable: no'
          END

        --  Whether the server marked the mailbox as possibly having new messages.
        EXEC sp_OAMethod @mailboxes, 'IsMarked', @iTmp0 OUT, @i
        IF @iTmp0
          BEGIN

            PRINT '  Marked: yes'
          END

        --  Whether the mailbox has, or may contain, child mailboxes.
        EXEC sp_OAMethod @mailboxes, 'HasInferiors', @iTmp0 OUT, @i
        IF @iTmp0
          BEGIN

            PRINT '  May have child mailboxes'
          END

        --  Check for a specific flag by name.  Include the leading backslash for a system flag;
        --  in CkScript a literal backslash in a string is written as "\\".
        EXEC sp_OAMethod @mailboxes, 'HasFlag', @iTmp0 OUT, @i, '\HasChildren'
        IF @iTmp0
          BEGIN

            PRINT '  Has child mailboxes'
          END

        --  Get all of the mailbox's flags as a comma-separated string.
        DECLARE @flags nvarchar(4000)
        EXEC sp_OAMethod @mailboxes, 'GetFlags', @flags OUT, @i

        PRINT '  Flags: ' + @flags

        --  Iterate the flags individually.
        DECLARE @numFlags int
        EXEC sp_OAMethod @mailboxes, 'GetNumFlags', @numFlags OUT, @i
        DECLARE @j int

        SELECT @j = 0
        WHILE @j <= @numFlags - 1
          BEGIN
            DECLARE @flag nvarchar(4000)
            EXEC sp_OAMethod @mailboxes, 'GetNthFlag', @flag OUT, @i, @j


            PRINT '    Flag ' + @j + ': ' + @flag
            SELECT @j = @j + 1
          END
        SELECT @i = @i + 1
      END

    --  Look up a mailbox by name.  Returns -1 if no mailbox with that name is present.
    DECLARE @sentIndex int
    EXEC sp_OAMethod @mailboxes, 'GetMailboxIndex', @sentIndex OUT, 'Sent'
    IF @sentIndex >= 0
      BEGIN

        PRINT 'The Sent mailbox is at index ' + @sentIndex
      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 @mailboxes
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @mailboxes


END
GO