Sample code for 30+ languages & platforms
SQL Server

Set or Clear a Flag on a MessageSet

See more IMAP Examples

Demonstrates the Chilkat Imap.SetFlags method, which sets or clears a flag on every message in a MessageSet. The first argument is the MessageSet, the second is the flag name, and the third is the value (1 to set the flag, 0 to clear it). This example marks all unseen messages as Seen.

Background: IMAP flags are per-message keywords the server stores and shares across all clients. The standard system flags are Seen, Answered, Flagged, Deleted, Draft, and Recent. Give the flag name without the leading backslash here (use "Seen", not "\Seen"). SetFlags applies the change to the whole set in one round trip, which is how a client implements "mark all as read."

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.SetFlags method, which sets or clears a flag on every message in a
    --  MessageSet.  The 1st argument is the MessageSet, the 2nd is the flag name, and the 3rd is
    --  the value (1 to set the flag, 0 to clear it).

    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 flag (here, all 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

    --  Mark all of them as seen.  A value of 1 sets the flag; 0 would clear it.
    EXEC sp_OAMethod @imap, 'SetFlags', @success OUT, @msgSet, 'Seen', 1
    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


    EXEC sp_OAGetProperty @msgSet, 'Count', @iTmp0 OUT

    PRINT 'Marked ' + @iTmp0 + ' messages as Seen.'

    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
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @msgSet


END
GO