Sample code for 30+ languages & platforms
SQL Server

Enumerate MIME Header Field Values

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldValue method, which returns the unfolded value of the header-field occurrence at a zero-based index. The only argument is the index. Continuation (folded) lines are combined into a single value.

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: Paired with GetHeaderFieldName, this lets you list every header as name/value pairs, including duplicates. A useful detail is unfolding: long headers are wrapped across multiple physical lines in the raw message, and this recombines them into the single logical value the header actually represents, so you never have to reassemble continuation lines yourself.

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

    --  Iterate the header fields by index and print each name and unfolded value.  Continuation lines
    --  (folded headers) are combined into a single value.
    DECLARE @n int
    EXEC sp_OAGetProperty @mime, 'NumHeaderFields', @n OUT
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        DECLARE @fieldName nvarchar(4000)
        EXEC sp_OAMethod @mime, 'GetHeaderFieldName', @fieldName OUT, @i
        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
        DECLARE @fieldValue nvarchar(4000)
        EXEC sp_OAMethod @mime, 'GetHeaderFieldValue', @fieldValue OUT, @i
        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 @fieldName + ': ' + @fieldValue
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @mime


END
GO