Sample code for 30+ languages & platforms
SQL Server

Access a Part in a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.PartAt method, which provides live access to the direct child at a zero-based index. The first argument is the index and the second is a Mime object that will reference the child. Modifying it modifies the child stored in the parent tree.

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: This is how you reach into a multipart message to inspect or edit an individual part — read a part's Content-Type, extract an attachment's body, or change a part in place. The key point is that the returned Mime is a live reference, not a copy, so edits to it flow back into the parent tree. Loop from 0 to NumParts - 1 to visit every direct child.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr 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

    --  PartAt provides LIVE access to a direct child part.  The 1st argument is the zero-based index
    --  and the 2nd is a Mime object that will reference the child.  Modifying it modifies the child in
    --  the parent tree.
    DECLARE @n int
    EXEC sp_OAGetProperty @mime, 'NumParts', @n OUT

    PRINT 'Number of parts: ' + @n

    DECLARE @part int
    EXEC @hr = sp_OACreate 'Chilkat.Mime', @part OUT

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @mime, 'PartAt', @success OUT, @i, @part
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @mime
            EXEC @hr = sp_OADestroy @part
            RETURN
          END


        EXEC sp_OAGetProperty @part, 'ContentType', @sTmp0 OUT
        PRINT 'Part ' + @i + ' Content-Type: ' + @sTmp0
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @part


END
GO