SQL Server
SQL Server
Fetch an IMAP Attachment into a BinData
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchAttachmentBd method, which stores one attachment's bytes in a BinData object. The first argument is the Email, the second is the zero-based attachment index, and the third is the BinData, which is cleared before the operation. This example fetches the first attachment and prints its byte count.
Background: A
BinData holds raw bytes, so it works for any attachment type — image, PDF, zip, and so on. From there you can save it, hash it, or hand it to another API expecting a byte buffer, all without touching the filesystem. Use the string/StringBuilder variants only when the attachment is known to be text.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.FetchAttachmentBd method, which downloads one attachment's bytes into
-- a BinData object. The 1st argument is the Email, the 2nd is the zero-based attachment
-- index, and the 3rd is the BinData (which is 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 just the first attachment's bytes from the server into a BinData.
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
EXEC sp_OAMethod @imap, 'FetchAttachmentBd', @success OUT, @email, 0, @bd
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 @bd
RETURN
END
EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT
PRINT 'Attachment size: ' + @iTmp0 + ' bytes'
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 @bd
RETURN
END
EXEC @hr = sp_OADestroy @imap
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @bd
END
GO