Sample code for 30+ languages & platforms
SQL Server

Add a Related String Resource to an Email

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedString method, which adds a text-based related MIME resource, such as a style sheet, from an in-memory string and returns the generated Content-ID. The first argument is the MIME resource name, the second is the text content, and the third is the charset used to encode it. Because the Content-ID is generated by the call, this example adds the style sheet first, then builds an HTML body in a StringBuilder that references the style sheet by that cid:, and passes the result to SetHtmlBody.

Background: Related items are not always images loaded from disk — sometimes the resource is text you already have in memory, like a generated CSS style sheet or an SVG. AddRelatedString lets you embed such content directly without writing a temporary file first. As with other related items, it lives in the multipart/related enclosure and is referenced from the HTML by the Content-ID the method returns.

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 AddRelatedString method, which adds a text-based related MIME resource
    --  (such as a style sheet) from an in-memory string and returns the generated Content-ID.
    --  The first argument is the resource name, the second is the text content, the third is the charset.

    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', 'Email with a related style sheet'

    --  Add the text-based related resource first; capture the generated Content-ID.
    DECLARE @cid nvarchar(4000)
    EXEC sp_OAMethod @email, 'AddRelatedString', @cid OUT, 'styles.css', 'body { color: navy; }', '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


    PRINT 'Related Content-ID = ' + @cid

    EXEC sp_OAGetProperty @email, 'NumRelatedItems', @iTmp0 OUT
    PRINT 'NumRelatedItems = ' + @iTmp0

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


END
GO