Sample code for 30+ languages & platforms
SQL Server

List IMAP Mailboxes

See more IMAP Examples

Demonstrates the Chilkat Imap.MbxList method, which lists matching mailboxes and appends them to a Mailboxes object. The first argument selects subscribed-only, the second is a reference name, the third is the mailbox name pattern, and the fourth is the Mailboxes object. This example lists all mailboxes and prints each name. (The object is not cleared on success, so avoid reusing one across calls to prevent duplicates.)

Background: This corresponds to the IMAP LIST command (or LSUB when subscribed-only is requested). The pattern uses IMAP wildcards: * matches across hierarchy levels while % matches within a single level, so Archive/% lists just the direct children of Archive. Each returned mailbox also carries flags you can inspect — for example HasInferiors (has sub-folders) and IsSelectable — which is how a client renders a folder tree.

Chilkat SQL Server Downloads

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

    --  Demonstrates the Imap.MbxList method, which lists matching mailboxes and appends them to a
    --  Mailboxes object.  The 1st argument selects subscribed-only, the 2nd is a reference name,
    --  the 3rd is the mailbox pattern, and the 4th is the Mailboxes object.

    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

    --  List all mailboxes.  The first argument 0 requests all (not subscribed-only); "*" matches any name.
    DECLARE @mailboxes int
    EXEC @hr = sp_OACreate 'Chilkat.Mailboxes', @mailboxes OUT

    EXEC sp_OAMethod @imap, 'MbxList', @success OUT, 0, '', '*', @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

    DECLARE @n int
    EXEC sp_OAGetProperty @mailboxes, 'Count', @n OUT

    PRINT 'Mailboxes: ' + @n
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @mailboxes, 'GetName', @sTmp0 OUT, @i
        PRINT @sTmp0
        SELECT @i = @i + 1
      END

    EXEC sp_OAGetProperty @imap, 'Disconnect', @success OUTERROR: ";" expected


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


END
GO