Sample code for 30+ languages & platforms
SQL Server

Enumerate MIME Header Field Names

See more MIME Examples

Demonstrates the Chilkat Mime.GetHeaderFieldName method, which returns the name of the header-field occurrence at a zero-based index. The only argument is the index; use NumHeaderFields for the count. Original order and casing are preserved.

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: When you need to walk all the headers — to display them, copy them, or handle duplicates that GetHeaderField would hide — index-based access is the way. NumHeaderFields bounds the loop, and this returns each field's name in its original order and casing, which matters when the exact header sequence is significant.

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.  NumHeaderFields gives the count; GetHeaderFieldName and
    --  GetHeaderFieldValue return the name and value at each zero-based index.  Original order and
    --  casing are preserved.
    DECLARE @n int
    EXEC sp_OAGetProperty @mime, 'NumHeaderFields', @n OUT

    PRINT 'Header fields: ' + @n
    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

        PRINT @fieldName
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @mime


END
GO