Sample code for 30+ languages & platforms
SQL Server

Get a MIME Header Field Attribute

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldAttribute method, which returns an attribute value parsed from a header field. The first argument is the header field name and the second is the attribute name. Surrounding quotes are removed.

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

Background: Many MIME headers pack extra parameters after the main value — Content-Type: multipart/mixed; boundary="xyz" or Content-Disposition: attachment; filename="report.pdf". This method extracts one of those named attributes directly, saving you from parsing the header string yourself and correctly stripping quotes and handling encoded values.

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
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

    --  Return an attribute value parsed from a header field.  The 1st argument is the header field
    --  name and the 2nd is the attribute name.  For example, the "boundary" attribute of the
    --  Content-Type header, or the "filename" attribute of Content-Disposition.
    DECLARE @boundary nvarchar(4000)
    EXEC sp_OAMethod @mime, 'GetHeaderFieldAttribute', @boundary OUT, 'Content-Type', 'boundary'
    EXEC sp_OAGetProperty @mime, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    PRINT 'Content-Type boundary: ' + @boundary

    EXEC @hr = sp_OADestroy @mime


END
GO