Sample code for 30+ languages & platforms
SQL Server

Remove a Header Field from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveHeaderField method, which removes by name all occurrences of a header field. Header-field names are case-insensitive, and every repeated occurrence with the given name is removed. This example adds a custom header, then removes it.

Background: Editing a message sometimes means deleting a header outright — stripping a tracking header, removing an X- flag before forwarding, or clearing a stale field before re-sending. Because a field name can appear more than once (like Received), RemoveHeaderField removes all matching occurrences in one call rather than just the first.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    --  Demonstrates the RemoveHeaderField method, which removes by name all occurrences of a
    --  header field.  Header-field names are case-insensitive.

    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', 'Remove header field'
    EXEC sp_OASetProperty @email, 'From', 'alice@example.com'

    EXEC sp_OAMethod @email, 'AddHeaderField', NULL, 'X-Custom-Header', 'some value'

    --  Use HasHeaderMatching to test for the header's presence.  ("*" matches any value.)
    DECLARE @hasBefore int
    EXEC sp_OAMethod @email, 'HasHeaderMatching', @hasBefore OUT, 'X-Custom-Header', '*', 0

    PRINT 'Has X-Custom-Header before: ' + @hasBefore

    --  Remove all occurrences of the header field.
    EXEC sp_OAMethod @email, 'RemoveHeaderField', NULL, 'X-Custom-Header'

    DECLARE @hasAfter int
    EXEC sp_OAMethod @email, 'HasHeaderMatching', @hasAfter OUT, 'X-Custom-Header', '*', 0

    PRINT 'Has X-Custom-Header after: ' + @hasAfter

    EXEC @hr = sp_OADestroy @email


END
GO