SQL Server
SQL Server
Fetch a Text IMAP Attachment into a StringBuilder
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchAttachmentSb method, which decodes one text attachment into a StringBuilder. The first argument is the Email, the second is the zero-based attachment index, the third is the charset used to decode the bytes, and the fourth is the StringBuilder, which is cleared first. This example decodes the first attachment as UTF-8.
Background: For text attachments — a CSV, a
.txt, an XML file — decoding straight to characters saves a manual charset step. The charset argument names the encoding of the attachment bytes (for example utf-8 or iso-8859-1). For binary attachments, use FetchAttachmentBd instead, since forcing binary through a text decoder can corrupt it.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 @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Demonstrates the Imap.FetchAttachmentSb method, which downloads one text attachment and
-- decodes it into a StringBuilder. The 1st argument is the Email, the 2nd is the zero-based
-- attachment index, the 3rd is the charset used to decode, and the 4th is the StringBuilder
-- (cleared first).
--
-- The message is fetched headers-only, then the attachment is downloaded on demand.
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
-- Fetch only the message headers. Attachment bodies are NOT downloaded, but the ckx-imap-*
-- metadata describing the attachments is present, so the attachment info methods still work.
DECLARE @headersOnly int
SELECT @headersOnly = 1
DECLARE @useUid int
SELECT @useUid = 0
DECLARE @seqNum int
SELECT @seqNum = 1
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
EXEC sp_OAMethod @imap, 'FetchEmail', @success OUT, @headersOnly, @seqNum, @useUid, @email
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @email
RETURN
END
DECLARE @numAttach int
EXEC sp_OAMethod @imap, 'GetMailNumAttach', @numAttach OUT, @email
IF @numAttach > 0
BEGIN
-- Download the first (text) attachment and decode it as UTF-8. Use this only for text.
DECLARE @sb int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sb OUT
EXEC sp_OAMethod @imap, 'FetchAttachmentSb', @success OUT, @email, 0, 'utf-8', @sb
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @sb
RETURN
END
DECLARE @attachText nvarchar(4000)
EXEC sp_OAMethod @sb, 'GetAsString', @attachText OUT
PRINT @attachText
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 @email
EXEC @hr = sp_OADestroy @sb
RETURN
END
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @sb
END
GO