Sample code for 30+ languages & platforms
SQL Server

Copy a Range of IMAP Messages

See more IMAP Examples

Demonstrates the Chilkat Imap.CopySequence method, which copies a contiguous range of messages, identified by sequence number, from the selected mailbox to a destination mailbox. The first argument is the starting sequence number and the second is the number of messages to copy. This example copies the first ten messages to another mailbox.

Background: Copying a whole range in a single command is far more efficient than copying messages one at a time — it's one server round trip instead of many. This method works by sequence number (position), so it is well suited to "copy the first N" or "copy messages 50–100" style operations. To copy an arbitrary, non-contiguous set of messages, build a MessageSet and use CopyMultiple.

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.CopySequence method, which copies a contiguous range of messages,
    --  identified by sequence number, from the selected mailbox to a destination mailbox.  The
    --  1st argument is the first sequence number and the 2nd is the number of messages to copy.

    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

    --  Copy the first 10 messages (sequence numbers 1 through 10) to "Archive2026".
    EXEC sp_OAMethod @imap, 'CopySequence', @success OUT, 1, 10, 'Archive2026'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END


    PRINT 'Copied the message range.'

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

    EXEC @hr = sp_OADestroy @imap


END
GO