Sample code for 30+ languages & platforms
SQL Server

Change the Filename of a Related Item

See more Email Object Examples

Demonstrates the Chilkat Email.SetRelatedFilename method, which sets (changes) the filename stored for a related item within the email. The index is zero-based. This example adds a related style sheet and renames it, printing the filename before and after.

Background: A related item's filename identifies the embedded resource within the message. Changing it is occasionally needed to match how the HTML body references the resource, or to give an embedded image or style sheet a cleaner name. Keep in mind that if the HTML links to the item by that name (via Content-Location), the reference should be kept in sync with the new filename.

Chilkat SQL Server Downloads

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

    --  Demonstrates the SetRelatedFilename method, which sets (changes) the filename of a
    --  related item within the email.  The index is zero-based.

    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', 'Set related item filename'

    --  Set an HTML body and add a related style sheet (index 0).
    EXEC sp_OAMethod @email, 'SetHtmlBody', NULL, '<html><head><link rel="stylesheet" href="styles.css"/></head><body>Styled.</body></html>'
    EXEC sp_OAMethod @email, 'AddRelatedString2', NULL, 'styles.css', 'body { color: navy; }', 'utf-8'

    EXEC sp_OAMethod @email, 'GetRelatedFilename', @sTmp0 OUT, 0
    PRINT 'Related filename before: ' + @sTmp0

    --  Change the filename of the related item at index 0.
    EXEC sp_OAMethod @email, 'SetRelatedFilename', @success OUT, 0, 'main.css'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAMethod @email, 'GetRelatedFilename', @sTmp0 OUT, 0
    PRINT 'Related filename after: ' + @sTmp0

    EXEC @hr = sp_OADestroy @email


END
GO