SQL Server
SQL Server
Fetch a Single Message's MIME into a StringBuilder
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a StringBuilder. The first argument is the message id, the second (bUid) selects UID vs sequence number, and the third is the StringBuilder, which is cleared first. This example downloads a message by UID and prints the MIME length.
Background: Chilkat interprets the received MIME as UTF-8. Messages using Base64 or quoted-printable transfer encoding are safe because their encoded bytes are ASCII. Raw
8bit or binary content, or unencoded text in a charset such as ISO-8859-1 or Shift_JIS, can be misinterpreted — in those cases use FetchSingleBd to get the exact bytes.Chilkat SQL Server Downloads
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the Imap.FetchSingleAsMimeSb method, which downloads one message's MIME into a
-- StringBuilder. The 1st argument is the message id, the 2nd (bUid) selects UID vs sequence
-- number, and the 3rd is the StringBuilder (cleared first).
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
-- Search for the message ids to download, returning UIDs.
DECLARE @wantUids int
SELECT @wantUids = 1
DECLARE @msgSet int
EXEC @hr = sp_OACreate 'Chilkat.MessageSet', @msgSet OUT
EXEC sp_OAMethod @imap, 'QueryMbx', @success OUT, 'ALL', @wantUids, @msgSet
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
RETURN
END
EXEC sp_OAGetProperty @msgSet, 'Count', @iTmp0 OUT
IF @iTmp0 > 0
BEGIN
DECLARE @uid int
EXEC sp_OAMethod @msgSet, 'GetId', @uid OUT, 0
-- Download the message's MIME into a StringBuilder. The id is a UID.
DECLARE @useUid int
SELECT @useUid = 1
DECLARE @sbMime int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT
EXEC sp_OAMethod @imap, 'FetchSingleAsMimeSb', @success OUT, @uid, @useUid, @sbMime
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @sbMime
RETURN
END
EXEC sp_OAGetProperty @sbMime, 'Length', @iTmp0 OUT
PRINT 'MIME length: ' + @iTmp0 + ' chars'
END
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
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @sbMime
RETURN
END
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @msgSet
EXEC @hr = sp_OADestroy @sbMime
END
GO