Sample code for 30+ languages & platforms
SQL Server

Prepend Added Header Fields to the Top

See more Email Object Examples

Demonstrates the Chilkat Email.PrependHeaders property. When true, header fields added through AddHeaderField or AddHeaderField2 are placed at the top of the header rather than appended at the bottom (the default is false). This affects only fields added after the property is set — it does not reorder fields that are already present. This example enables prepending and adds two custom header fields.

Background: In a MIME message the order of header fields is mostly cosmetic, but there are cases where "top vs bottom" matters — for instance, trace headers like Received are conventionally added to the top so the most recent hop appears first, and some pipelines expect certain fields early in the header block. PrependHeaders gives you control over placement when you add your own custom fields.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Demonstrates the Email.PrependHeaders property.  When true, header fields added via
    --  AddHeaderField (or AddHeaderField2) are prepended to the top of the header instead of
    --  appended to the bottom.  The default is false.

    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', 'Prepend headers demo'
    EXEC sp_OASetProperty @email, 'From', 'mary@example.com'

    --  Cause subsequently-added header fields to go to the top of the header.
    EXEC sp_OASetProperty @email, 'PrependHeaders', 1

    EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'X-Custom-One', 'first value'
    EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'X-Custom-Two', 'second value'

    EXEC sp_OAGetProperty @email, 'Header', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @email


END
GO