SQL Server
SQL Server
How to Copy IMAP Mail to another IMAP Server
Demonstrates how to copy the entire contents of an IMAP mailbox to another IMAP server.Chilkat SQL Server Downloads
-- 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
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @imapSrc int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imapSrc OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Connect to our source IMAP server.
EXEC sp_OAMethod @imapSrc, 'Connect', @success OUT, 'imap.example.com'
IF @success = 0
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, 'admin@chilkatsoft.com', 'myPassword'
IF @success = 0
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_OAMethod @imapDest, 'Connect', @success OUT, 'mail.example-code.com'
IF @success = 0
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, 'myLogin', 'myPassword'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imapSrc
EXEC @hr = sp_OADestroy @imapDest
RETURN
END
-- Select an IMAP mailbox on the source IMAP server
EXEC sp_OAMethod @imapSrc, 'SelectMailbox', @success OUT, 'Inbox'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imapSrc, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imapSrc
EXEC @hr = sp_OADestroy @imapDest
RETURN
END
-- After selecting a mailbox, the NumMessages property will
-- be updated to reflect the total number of emails in the mailbox:
EXEC sp_OAGetProperty @imapSrc, 'NumMessages', @iTmp0 OUT
PRINT @iTmp0
-- The emails in the mailbox will always have sequence numbers
-- ranging from 1 to NumMessages.
-- This example will copy the first 10 messages using sequence numbers
DECLARE @sbMime int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT
DECLARE @seqNum int
SELECT @seqNum = 1
WHILE @seqNum <= 10
BEGIN
EXEC sp_OAMethod @sbMime, 'Clear', NULL
EXEC sp_OAMethod @imapSrc, 'FetchSingleAsMimeSb', @success OUT, @seqNum, 0, @sbMime
IF @success = 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 @sbMime
RETURN
END
EXEC sp_OAMethod @imapDest, 'AppendMimeWithFlagsSb', @success OUT, 'Inbox', @sbMime, 0, 0, 0, 0
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imapDest, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imapSrc
EXEC @hr = sp_OADestroy @imapDest
EXEC @hr = sp_OADestroy @sbMime
RETURN
END
SELECT @seqNum = @seqNum + 1
END
-- 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 @sbMime
END
GO