Sample code for 30+ languages & platforms
SQL Server

Get the Decoded MIME Body Bytes (BinData)

See more MIME Examples

Demonstrates the Chilkat Mime.GetBodyBd method, which replaces the contents of a BinData with the decoded body bytes of the current part. The only argument is the BinData. Base64 and quoted-printable are decoded; headers and boundaries are not included.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is how you extract a part's actual content as bytes — the decoded payload of an attachment or an image, ready to hash, re-save, or hand to another API. Because it works in raw bytes it is the correct choice for binary parts, where a text decode would be wrong. Note it replaces the BinData contents rather than appending.

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

    DECLARE @mime int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'qa_data/message.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  Replace the contents of a BinData with the decoded body bytes of the current part.  Base64 and
    --  quoted-printable are decoded; MIME headers and multipart boundaries are not included.
    DECLARE @bd int
    EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT

    EXEC sp_OAMethod @mime, 'GetBodyBd', @success OUT, @bd
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @bd
        RETURN
      END

    EXEC sp_OAGetProperty @bd, 'NumBytes', @iTmp0 OUT

    PRINT 'Decoded body: ' + @iTmp0 + ' bytes.'

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @bd


END
GO