Sample code for 30+ languages & platforms
SQL Server

Get the Filename of an Attached Message

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedMessageFilename method, which returns the filename of the Nth attached (embedded) email. The filename is the filename attribute of the attached message's Content-Disposition header, and the index is zero-based. This example attaches an email and reads the attached message's filename.

Background: When an email forwards another message as an attachment, the nested message is a message/rfc822 part — a complete email tucked inside the carrier. Mail clients often give that part a filename (like forwarded.eml) via its Content-Disposition header so it can be saved to disk. This method reads that filename directly. Attached messages are counted separately from ordinary attachments (see NumAttachedMessages).

Chilkat SQL Server Downloads

SQL Server
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the GetAttachedMessageFilename method, which returns the filename of the
    --  Nth attached (embedded) email.  The filename is the "filename" attribute of the
    --  attached message's Content-Disposition header.  The index is zero-based.

    --  Build an inner email to attach.
    DECLARE @innerEmail int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @innerEmail OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @innerEmail, 'Subject', 'Embedded message'
    EXEC sp_OASetProperty @innerEmail, 'From', 'alice@example.com'
    EXEC sp_OAMethod @innerEmail, 'AddTo', @success OUT, 'Bob', 'bob@example.com'

    --  Attach it to an outer email as a message/rfc822 part.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Has an attached message'
    EXEC sp_OAMethod @email, 'AttachEmail', @success OUT, @innerEmail
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @innerEmail
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Get the filename of the first attached message (index 0).
    DECLARE @fname nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetAttachedMessageFilename', @fname OUT, 0

    PRINT 'Attached message filename: ' + @fname

    EXEC @hr = sp_OADestroy @innerEmail
    EXEC @hr = sp_OADestroy @email


END
GO