Sample code for 30+ languages & platforms
SQL Server

Remove a Part from a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.RemovePart method, which removes the direct child at a zero-based index. The only argument is the index; valid values are 0 through NumParts - 1. An out-of-range index returns false and changes nothing.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: Removing a part is how you strip an attachment or drop an unwanted alternative when reprocessing a message. Because parts are addressed by position, be mindful that removing one shifts the indexes of those after it — when deleting several, iterate from the last index downward so earlier removals do not renumber the parts you still intend to remove.

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

    DECLARE @mime int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @mime OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @mime, 'LoadMimeFile', @success OUT, 'qa_data/multipart_message.eml'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END

    --  Remove the direct child part at a zero-based index.  Valid indexes are 0 through NumParts - 1.
    --  Here the last part is removed.
    DECLARE @lastIndex int
    EXEC sp_OAGetProperty @mime, 'NumParts', @iTmp0 OUT
    SELECT @lastIndex = @iTmp0 - 1
    EXEC sp_OAMethod @mime, 'RemovePart', @success OUT, @lastIndex
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        RETURN
      END


    EXEC sp_OAGetProperty @mime, 'NumParts', @iTmp0 OUT
    PRINT 'Parts remaining: ' + @iTmp0

    EXEC @hr = sp_OADestroy @mime


END
GO