Sample code for 30+ languages & platforms
SQL Server

Save All MIME Attachments to Files

See more MIME Examples

Demonstrates the Chilkat Mime.PartsToFiles method, which recursively traverses the MIME tree and saves each part having a nonempty filename parameter to a directory. The first argument is the destination directory and the second is a StringTable that receives the saved filenames.

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 extracts every named attachment from a message in one call, walking the whole tree so it finds attachments nested inside multipart containers. It saves only parts that declare a filename — inline body parts without one are skipped — and reports what it wrote via the StringTable, which is handy for logging or follow-up processing. The destination directory must already exist.

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 @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

    --  Recursively traverse the MIME tree and save each part that has a nonempty "filename" parameter
    --  to a directory.  The 1st argument is the destination directory and the 2nd is a StringTable
    --  that receives the saved filenames.
    DECLARE @savedFiles int
    EXEC @hr = sp_OACreate 'Chilkat.StringTable', @savedFiles OUT

    EXEC sp_OAMethod @mime, 'PartsToFiles', @success OUT, 'qa_output/attachments', @savedFiles
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mime, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mime
        EXEC @hr = sp_OADestroy @savedFiles
        RETURN
      END

    DECLARE @n int
    EXEC sp_OAGetProperty @savedFiles, 'Count', @n OUT


    PRINT 'Saved ' + @n + ' files:'
    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        DECLARE @name nvarchar(4000)
        EXEC sp_OAMethod @savedFiles, 'StringAt', @name OUT, @i
        EXEC sp_OAGetProperty @savedFiles, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @savedFiles, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @mime
            EXEC @hr = sp_OADestroy @savedFiles
            RETURN
          END

        PRINT @name
        SELECT @i = @i + 1
      END

    EXEC @hr = sp_OADestroy @mime
    EXEC @hr = sp_OADestroy @savedFiles


END
GO