Sample code for 30+ languages & platforms
SQL Server

Change the Filename of an Attachment

See more Email Object Examples

Demonstrates the Chilkat Email.SetAttachmentFilename method, which changes the filename of the attachment at a given zero-based index. This example adds an attachment and renames it, printing the filename before and after.

Background: The attachment filename is what a recipient sees and what most clients suggest when saving the file. Renaming it is handy when the original name is unclear, unsafe, or generic — for example giving a machine-generated tmp12345 a meaningful name like invoice.pdf before sending.

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 SetAttachmentFilename method, which changes the filename of the
    --  attachment at the given 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', 'Set attachment filename'

    EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'oldname.txt', 'Some notes.'

    EXEC sp_OAMethod @email, 'GetAttachmentFilename', @sTmp0 OUT, 0
    PRINT 'Filename before: ' + @sTmp0

    --  Change the filename of the first attachment (index 0).
    EXEC sp_OAMethod @email, 'SetAttachmentFilename', @success OUT, 0, 'newname.txt'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @email
        RETURN
      END


    EXEC sp_OAMethod @email, 'GetAttachmentFilename', @sTmp0 OUT, 0
    PRINT 'Filename after: ' + @sTmp0

    EXEC @hr = sp_OADestroy @email


END
GO