SQL Server
SQL Server
Add a Related File to an HTML Email
See more Email Object Examples
Demonstrates the Chilkat Email.AddRelatedFile method, which adds the contents of a local file as a related MIME resource and returns the generated Content-ID. The returned value is the bare Content-ID — no angle brackets and no cid: prefix — so if it returns CID-123@example, the HTML reference is cid:CID-123@example. Because the Content-ID is generated by the call, this example adds the image first, then builds the HTML body in a StringBuilder — using a placeholder in the <img> tag that is replaced with the returned Content-ID — and passes the result to SetHtmlBody.
Background: To embed an image so it displays inside an HTML email (rather than being downloaded from a web server), you add it as a "related" part and reference it from the HTML with a
cid: URL that matches the part's Content-ID. Since Chilkat assigns that ID when you add the file, the natural order is: add the related file, capture the returned ID, then build the <img src="cid:..."> reference from it. A StringBuilder makes assembling and updating the HTML convenient.Chilkat SQL Server Downloads
-- 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 AddRelatedFile method, which adds a local file as a related MIME
-- resource (such as an image displayed by an HTML body) and returns the generated
-- Content-ID. The HTML references the item using cid:<Content-ID>.
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 image'
-- Add the image file as a related item first. The return value is the bare Content-ID
-- (no angle brackets and no "cid:" prefix).
DECLARE @cid nvarchar(4000)
EXEC sp_OAMethod @email, 'AddRelatedFile', @cid OUT, 'qa_data/images/logo.png'
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 in a StringBuilder, using a placeholder where the image's
-- Content-ID will go.
DECLARE @sbHtml int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbHtml OUT
DECLARE @success int
EXEC sp_OAMethod @sbHtml, 'Append', @success OUT, '<html><body><img src="cid:PLACEHOLDER_CID"/></body></html>'
-- Replace the placeholder with the actual Content-ID returned by AddRelatedFile.
DECLARE @numReplaced int
EXEC sp_OAMethod @sbHtml, 'Replace', @numReplaced OUT, 'PLACEHOLDER_CID', @cid
-- Set the HTML body from the StringBuilder result.
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
-- Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
-- relative to the current working directory of the running application.
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @sbHtml
END
GO