Sample code for 30+ languages & platforms
SQL Server

Add a Custom Header to a Related Item

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedHeader method, which adds a custom MIME header field to an existing related item identified by its zero-based index. This example first adds a related style sheet (which becomes index 0) and captures its generated Content-ID, builds an HTML body that references the style sheet by that cid:, then attaches an extra header field to the related item and prints the resulting MIME.

Background: Like attachments, each related item (an inline image, style sheet, etc.) is a MIME part with its own small header block describing that part — Content-Type, Content-ID, Content-Location, and so on. AddRelatedHeader lets you insert additional fields into that per-part block, which is occasionally required for interoperability with clients that look for specific custom headers on embedded resources.

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)
    --  Demonstrates the AddRelatedHeader method, which adds a custom MIME header field to an
    --  existing related item, identified by its zero-based index.

    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', 'Related item with a custom header'

    --  Add a related item first (a style sheet); capture its generated Content-ID.  It becomes
    --  related-item index 0.
    DECLARE @cid nvarchar(4000)
    EXEC sp_OAMethod @email, 'AddRelatedString', @cid OUT, 'styles.css', 'body { color: black; }', 'utf-8'
    EXEC sp_OAGetProperty @email, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Build the HTML body, referencing the related style sheet by its Content-ID.  A
    --  placeholder is used and then replaced with the actual Content-ID.
    DECLARE @sbHtml int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT

    DECLARE @success int
    EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '<html><head><link rel="stylesheet" href="cid:PLACEHOLDER_CID"/></head><body>Styled content.</body></html>'
    DECLARE @numReplaced int
    EXEC sp_OAMethod @sbHtml, 'Replace', @numReplaced OUT, 'PLACEHOLDER_CID', @cid
    EXEC sp_OAMethod @sbHtml, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, @sTmp0

    --  Add a custom header field to the first related item (index 0).
    EXEC sp_OAMethod @email, 'AddRelatedHeader', NULL, 0, 'X-Custom-Related-Header', 'some value'

    --  The custom header now appears in the related item's MIME part.
    EXEC sp_OAMethod @email, 'GetMime', @sTmp0 OUT
    PRINT @sTmp0

    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @sbHtml


END
GO