Sample code for 30+ languages & platforms
SQL Server

Add a Related File Addressed by Content-Location

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedFile2 method, which adds a local file as a related MIME resource addressed by Content-Location rather than Content-ID. The second argument becomes the related item's MIME filename and Content-Location value, and it should match the filename, path, or URL already used by the corresponding HTML reference (such as an img element's src). This example embeds an image referenced as logo.png.

Background: There are two ways HTML email links to embedded resources. The cid: approach (see AddRelatedFile) uses a generated Content-ID, while the Content-Location approach lets the HTML keep an ordinary-looking src="logo.png" and matches it to a part carrying the same Content-Location. The latter is handy when converting an existing web page to email, because the original relative URLs can stay unchanged.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the AddRelatedFile2 method, which adds a local file as a related MIME
    --  resource addressed by Content-Location rather than Content-ID.  The 2nd argument becomes
    --  the item's MIME filename / Content-Location and should match the reference in the HTML.

    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 (by Content-Location)'

    --  The HTML references the image by the same name used as the Content-Location.
    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><body><img src="logo.png"/></body></html>'

    --  Add the file as a related item addressed by Content-Location "logo.png".
    EXEC sp_OAMethod @email, 'AddRelatedFile2', @success OUT, 'qa_data/images/logo.png', 'logo.png'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    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


END
GO