Sample code for 30+ languages & platforms
SQL Server

Load a MessageSet from a Compact String

See more IMAP Examples

Demonstrates the Chilkat MessageSet.FromCompactString method, which clears the set and loads message identifiers from compact IMAP message-set syntax. The only argument is the compact string: a colon specifies an inclusive ascending range, and commas separate values or ranges. This example loads 1:5,8:10 and prints the result as a comma-separated list.

Background: Compact message-set syntax is how IMAP itself expresses groups of messages — 1:5,8:10 is far more concise than listing every id. FromCompactString is the inverse of ToCompactString: it parses that text into a set you can pass to Imap methods. Duplicates are removed, identifiers are sorted ascending, and the existing HasUids setting is preserved. Note the string carries only the numbers — it does not record whether they are UIDs or sequence numbers.

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 the MessageSet.FromCompactString method, which loads a set of message
    --  identifiers from compact IMAP message-set syntax.  The only argument is the compact string.
    --  A colon specifies an inclusive range, and commas separate values or ranges.

    DECLARE @msgSet int
    EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @msgSet OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Load a compact message set.  "1:5,8:10" expands to 1,2,3,4,5,8,9,10.
    EXEC sp_OAMethod @msgSet, 'FromCompactString', @success OUT, '1:5,8:10'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @msgSet, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @msgSet
        RETURN
      END


    EXEC sp_OAGetProperty @msgSet, 'Count', @iTmp0 OUT
    PRINT 'Count: ' + @iTmp0

    --  Show the loaded identifiers in comma-separated form.
    DECLARE @csv nvarchar(4000)
    EXEC sp_OAMethod @msgSet, 'ToCommaSeparatedStr', @csv OUT

    PRINT @csv

    EXEC @hr = sp_OADestroy @msgSet


END
GO