SQL Server
SQL Server
List Email UIDs
List the UIDs of each email in a mailbox.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
DECLARE @imap int
EXEC @hr = sp_OACreate 'Chilkat.Imap', @imap 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 an IMAP server.
-- Use TLS
EXEC sp_OASetProperty @imap, 'Ssl', 1
EXEC sp_OASetProperty @imap, 'Port', 993
EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'MY-IMAP-DOMAIN'
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, 'MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD'
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'
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
RETURN
END
-- Get the message IDs of all the emails in the mailbox
-- We can choose to fetch UIDs or sequence numbers.
DECLARE @fetchUids int
SELECT @fetchUids = 1
DECLARE @messageSet int
EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @messageSet OUT
EXEC sp_OAMethod @imap, 'QueryMbx', @success OUT, 'ALL', @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
DECLARE @i int
SELECT @i = 0
DECLARE @n int
EXEC sp_OAGetProperty @messageSet, 'Count', @n OUT
WHILE @i < @n
BEGIN
EXEC sp_OAMethod @messageSet, 'GetId', @iTmp0 OUT, @i
PRINT @iTmp0
SELECT @i = @i + 1
END
-- Disconnect from the IMAP server.
EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @messageSet
END
GO