SQL Server
SQL Server
Copy an Email from One Mailbox to Another
Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.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.
-- This example copies an email from one mailbox to another.
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Turn on session logging:
EXEC sp_OASetProperty @imap, 'KeepSessionLog', 1
-- Connect to an IMAP server.
-- Use TLS
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
-- Login
EXEC sp_OAMethod @imap, 'Login', @success OUT, '***', '***'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Select an IMAP mailbox
EXEC sp_OAMethod @imap, 'SelectMailbox', @success OUT, 'Inbox.testing.a'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
DECLARE @fetchUids int
SELECT @fetchUids = 1
-- Get the message IDs for all emails having "Re:" in the subject.
DECLARE @messageSet int
EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @messageSet OUT
EXEC sp_OAMethod @imap, 'QueryMbx', @success OUT, 'SUBJECT Re:', @fetchUids, @messageSet
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
RETURN
END
-- Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
EXEC sp_OAMethod @imap, 'CopyMultiple', @success OUT, @messageSet, 'Inbox.testing.b'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
RETURN
END
-- Alternatively, loop over each message in the set and
-- copy each separately:
DECLARE @i int
SELECT @i = 0
EXEC sp_OAGetProperty @messageSet, 'Count', @iTmp0 OUT
WHILE @i < @iTmp0
BEGIN
DECLARE @msgId int
EXEC sp_OAMethod @messageSet, 'GetId', @msgId OUT, @i
DECLARE @isUid int
EXEC sp_OAGetProperty @messageSet, 'HasUids', @isUid OUT
EXEC sp_OAMethod @imap, 'Copy', @success OUT, @msgId, @isUid, 'Inbox.testing.c'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
RETURN
END
SELECT @i = @i + 1
END
-- Display the session log.
EXEC sp_OAGetProperty @imap, 'SessionLog', @sTmp0 OUT
PRINT @sTmp0
-- Disconnect from the IMAP server.
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
END
GO