Sample code for 30+ languages & platforms
SQL Server

Get the Name of the Nth Header Field

See more Email Object Examples

Demonstrates the Chilkat Email.GetHeaderFieldName method, which returns the name of the Nth header field. The NumHeaderFields property gives the count, and indexing is zero-based. This example enumerates the names of all header fields.

Background: Enumerating headers by index — rather than looking them up by name — lets you discover which fields a message actually contains and see repeated fields (like multiple Received lines) in order. Pair GetHeaderFieldName with GetHeaderFieldValue at the same index to walk the entire header block as name/value pairs.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Demonstrates the GetHeaderFieldName method, which returns the name of the Nth header
    --  field.  The NumHeaderFields property gives the number of header fields; indexing is
    --  zero-based.

    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', 'Enumerate header names'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'

    DECLARE @n int
    EXEC sp_OAGetProperty @email, 'NumHeaderFields', @n OUT
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN


        EXEC sp_OAMethod @email, 'GetHeaderFieldName', @sTmp0 OUT, @i
        PRINT 'Header ' + @i + ' name: ' + @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @email


END
GO