Sample code for 30+ languages & platforms
SQL Server

Copy Email from one IMAP Account to Another

See more IMAP Examples

Demonstrates how to copy the email in a mailbox from one account to another.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Connect to our source IMAP server.
    EXEC sp_OASetProperty @imapSrc, 'Ssl', 1
    EXEC sp_OASetProperty @imapSrc, 'Port', 993
    EXEC sp_OAMethod @imapSrc, 'Connect', @success OUT, 'MY-IMAP-DOMAIN'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        RETURN
      END

    -- Login to the source IMAP server
    EXEC sp_OAMethod @imapSrc, 'Login', @success OUT, 'MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        RETURN
      END

    DECLARE @imapDest int
    EXEC @hr = sp_OACreate 'Chilkat.Imap', @imapDest OUT

    -- Connect to our destination IMAP server.
    EXEC sp_OASetProperty @imapDest, 'Ssl', 1
    EXEC sp_OASetProperty @imapDest, 'Port', 993
    EXEC sp_OAMethod @imapDest, 'Connect', @success OUT, 'MY-IMAP-DOMAIN2'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Login to the destination IMAP server
    EXEC sp_OAMethod @imapDest, 'Login', @success OUT, 'MY-IMAP-LOGIN2', 'MY-IMAP-PASSWORD2'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Select a source IMAP mailbox on the source IMAP server
    EXEC sp_OAMethod @imapSrc, 'SelectMailbox', @success OUT, 'Inbox'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    DECLARE @fetchUids int
    SELECT @fetchUids = 1

    -- Get the set of UIDs for all emails on the source server.
    DECLARE @mset int
    EXEC sp_OAMethod @imapSrc, 'Search', @mset OUT, 'ALL', @fetchUids
    EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imapSrc
        EXEC @hr = sp_OADestroy @imapDest
        RETURN
      END

    -- Load the complete set of UIDs that were previously copied.
    -- We dont' want to copy any of these to the destination.
    DECLARE @fac int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT

    DECLARE @msetAlreadyCopied int
    EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @msetAlreadyCopied OUT

    DECLARE @strMsgSet nvarchar(4000)
    EXEC sp_OAMethod @fac, 'ReadEntireTextFile', @strMsgSet OUT, 'qa_cache/saAlreadyLoaded.txt', 'utf-8'
    EXEC sp_OAGetProperty @fac, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @msetAlreadyCopied, 'FromCompactString', @success OUT, @strMsgSet
      END

    DECLARE @numUids int
    EXEC sp_OAGetProperty @mset, 'Count', @numUids OUT
    DECLARE @sbFlags int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbFlags OUT

    DECLARE @i int
    SELECT @i = 0
    WHILE @i < @numUids
      BEGIN

        -- If this UID was not already copied...
        DECLARE @uid int
        EXEC sp_OAMethod @mset, 'GetId', @uid OUT, @i
        EXEC sp_OAMethod @msetAlreadyCopied, 'ContainsId', @iTmp0 OUT, @uid
        IF Not @iTmp0
          BEGIN



            PRINT 'copying ' + @uid + '...'

            -- Get the flags.
            DECLARE @flags nvarchar(4000)
            EXEC sp_OAMethod @imapSrc, 'FetchFlags', @flags OUT, @uid, 1
            EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 = 0
              BEGIN
                EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END
            EXEC sp_OAMethod @sbFlags, 'SetString', @success OUT, @flags

            -- Get the MIME of this email from the source.
            DECLARE @mimeStr nvarchar(4000)
            EXEC sp_OAMethod @imapSrc, 'FetchSingleAsMime', @mimeStr OUT, @uid, 1
            EXEC sp_OAGetProperty @imapSrc, 'LastMethodSuccess', @iTmp0 OUT
            IF @iTmp0 = 0
              BEGIN
                EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END

            DECLARE @seen int
            EXEC sp_OAMethod @sbFlags, 'Contains', @seen OUT, '\Seen', 0
            DECLARE @flagged int
            EXEC sp_OAMethod @sbFlags, 'Contains', @flagged OUT, '\Flagged', 0
            DECLARE @answered int
            EXEC sp_OAMethod @sbFlags, 'Contains', @answered OUT, '\Answered', 0
            DECLARE @draft int
            EXEC sp_OAMethod @sbFlags, 'Contains', @draft OUT, '\Draft', 0

            EXEC sp_OAMethod @imapDest, 'AppendMimeWithFlags', @success OUT, 'Inbox', @mimeStr, @seen, @flagged, @answered, @draft
            IF @success <> 1
              BEGIN
                EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
                PRINT @sTmp0
                EXEC @hr = sp_OADestroy @imapSrc
                EXEC @hr = sp_OADestroy @imapDest
                EXEC @hr = sp_OADestroy @fac
                EXEC @hr = sp_OADestroy @msetAlreadyCopied
                EXEC @hr = sp_OADestroy @sbFlags
                RETURN
              END

            -- Update msetAlreadyCopied with the uid just copied.
            EXEC sp_OAMethod @msetAlreadyCopied, 'InsertId', NULL, @uid

            -- Save at every iteration just in case there's a failure..
            EXEC sp_OAMethod @msetAlreadyCopied, 'ToCompactString', @strMsgSet OUT
            EXEC sp_OAMethod @fac, 'WriteEntireTextFile', @success OUT, 'qa_cache/saAlreadyLoaded.txt', @strMsgSet, 'utf-8', 0
          END

        SELECT @i = @i + 1
      END
    EXEC @hr = sp_OADestroy @mset

    -- Disconnect from the IMAP servers.
    EXEC sp_OAMethod @imapSrc, 'Disconnect', @success OUT
    EXEC sp_OAMethod @imapDest, 'Disconnect', @success OUT

    EXEC @hr = sp_OADestroy @imapSrc
    EXEC @hr = sp_OADestroy @imapDest
    EXEC @hr = sp_OADestroy @fac
    EXEC @hr = sp_OADestroy @msetAlreadyCopied
    EXEC @hr = sp_OADestroy @sbFlags


END
GO