SQL Server
SQL Server
Get an Attachment's Bytes into a BinData
See more Email Object Examples
Demonstrates the Chilkat Email.GetAttachmentBd method, which copies an attachment's binary data into a BinData object. The first attachment is at index 0. This example adds an attachment and copies its bytes into a BinData, printing the byte count.
Background: This is the safe, binary way to extract an attachment — the counterpart to the text-oriented
GetAttachmentString. Because attachments are often binary (PDFs, images, archives), copying the raw bytes into a BinData preserves them exactly, ready to write to a file, hash, or pass to another API without any charset conversion that could corrupt the data.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 GetAttachmentBd method, which copies an attachment's binary data into a
-- BinData object. The first attachment is at index 0.
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @email, 'Subject', 'GetAttachmentBd example'
EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'notes.txt', 'Some notes stored in the attachment.'
-- Copy the first attachment's binary data into a BinData object.
DECLARE @bd int
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
EXEC sp_OAMethod @email, 'GetAttachmentBd', @success OUT, 0, @bd
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @bd
RETURN
END
EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT
PRINT 'Attachment size (bytes) = ' + @iTmp0
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @bd
END
GO