Sample code for 30+ languages & platforms
SQL Server

Get the Nth Binary Part of a Content-Type into BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth MIME sub-part matching a content-type pattern into a BinData object. The arguments are the zero-based index among the matching parts, the content-type pattern, an inlineOnly flag, an excludeAttachments flag, and the BinData that receives the bytes. This example extracts the first image/png part.

Background: This is the binary, type-targeted way to pull a specific part out of a message — ideal for extracting, say, every image/png or the one application/pdf from a complex MIME tree without caring whether it is an attachment, an inline image, or a body part. Reading into a BinData keeps the raw bytes exact, ready to save, hash, or re-transmit.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth
    --  MIME sub-part matching a Content-Type pattern into a BinData object.  The arguments are
    --  the zero-based index among matching parts, the Content-Type pattern, inlineOnly,
    --  excludeAttachments, and the BinData that receives the bytes.

    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', 'GetNthBinaryPartOfTypeBd example'
    EXEC sp_OASetProperty @email, 'Body', 'See the attached image.'

    --  Load the image from a file into a BinData object and attach it (binary data belongs
    --  in a BinData, never in a string).
    DECLARE @bdImage int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bdImage OUT

    EXEC sp_OAMethod @bdImage, 'LoadFile', @success OUT, 'qa_data/images/photo.png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @bdImage, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        RETURN
      END
    EXEC sp_OAMethod @email, 'AddAttachmentBd', @success OUT, 'photo.png', @bdImage, 'image/png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        RETURN
      END

    --  Load the bytes of the first (index 0) image/png part into a BinData object.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @email, 'GetNthBinaryPartOfTypeBd', @success OUT, 0, 'image/png', 0, 0, @bd
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @bdImage
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END


    EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT
    PRINT 'image/png part size (bytes) = ' + @iTmp0

    --  Note: The path "qa_data/images/photo.png" is a relative local filesystem path,
    --  relative to the current working directory of the running application.

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @bdImage
    EXEC @hr = sp_OADestroy @bd


END
GO