Sample code for 30+ languages & platforms
SQL Server

Remove a Single Attached Message from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.RemoveAttachedMessage method, which removes the Nth message/rfc822 sub-part (an attached email) from the message. Indexing begins at 0. This example attaches an email, removes it, and prints the attached-message count before and after.

Background: Attached messages (forwarded emails carried as message/rfc822 parts) are counted and indexed separately from ordinary file attachments and related items. RemoveAttachedMessage targets just one nested email by index — useful, for example, when trimming a bundled digest or stripping a forwarded original before re-sending.

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 RemoveAttachedMessage method, which removes the Nth message/rfc822
    --  sub-part (attached email) of the email.  The index is zero-based.

    --  Build an inner email and attach it to an outer email.
    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'

    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


    EXEC sp_OAGetProperty @email, 'NumAttachedMessages', @iTmp0 OUT
    PRINT 'NumAttachedMessages before = ' + @iTmp0

    --  Remove the first attached message (index 0).
    EXEC sp_OAMethod @email, 'RemoveAttachedMessage', NULL, 0


    EXEC sp_OAGetProperty @email, 'NumAttachedMessages', @iTmp0 OUT
    PRINT 'NumAttachedMessages after = ' + @iTmp0

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


END
GO