Sample code for 30+ languages & platforms
SQL Server

Strip Path Info from Attachment Filenames

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachmentPaths method, which removes relative or absolute path information from the attachment filenames stored in the MIME, leaving only each final filename component. This example adds an attachment whose name includes a path and shows the filename before and after.

Background: An attachment filename that carries a path (like reports/2026/q3/summary.txt) is a problem: some mail clients display the whole path, and — more seriously — a path with .. segments can be a directory-traversal risk when attachments are saved to disk. Reducing every filename to just its final component (summary.txt) makes the message cleaner and safer to handle.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  Demonstrates the RemoveAttachmentPaths method, which removes relative or absolute path
    --  information from attachment filenames stored in the MIME, leaving only each final
    --  filename component.

    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', 'Remove attachment paths'

    --  Add an attachment whose filename includes path information.
    DECLARE @success int
    EXEC sp_OAMethod @email, 'AddStringAttachment', @success OUT, 'reports/2026/q3/summary.txt', 'Attachment content.'

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

    --  Strip the path, leaving only the final filename component.
    EXEC sp_OAMethod @email, 'RemoveAttachmentPaths', NULL

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

    EXEC @hr = sp_OADestroy @email


END
GO