Sample code for 30+ languages & platforms
SQL Server

Save an IMAP Attachment to a File

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachment method, which downloads one attachment from the IMAP server and saves it to the filesystem. The first argument is the Email, the second is the zero-based attachment index, and the third is the file path to write. This example fetches a message headers-only, inspects its attachments without downloading them, and then downloads a specific attachment on demand.

Note: The output path is relative to the application's current working directory. Chilkat does not create missing parent directories, so qa_output/ must already exist.

Background: This is the method's real value. A headers-only fetch downloads no attachment bodies, yet methods such as GetMailNumAttach and GetMailAttachFilename still work from the ckx-imap-* metadata. FetchAttachment then uses that metadata to download just the requested MIME part from the server — even in a later session. A client can therefore list messages and their attachment names cheaply and pull individual attachments only when the user actually wants them, instead of downloading every full message. (If the Email already holds the attachment bytes, they are saved without contacting the server.)

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

    --  Demonstrates the Imap.FetchAttachment method, which downloads one attachment from the IMAP
    --  server and saves it to a file.  The 1st argument is the Email, the 2nd is the zero-based
    --  attachment index, and the 3rd is the file path to write.
    --  
    --  The compelling use case: fetch a message headers-only, inspect its attachments without
    --  downloading them, and then download just the attachment(s) you want.

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

    EXEC sp_OASetProperty @imap, 'Ssl', 1
    EXEC sp_OASetProperty @imap, 'Port', 993

    EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'imap.example.com'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END
    EXEC sp_OAMethod @imap, 'Login', @success OUT, 'user@example.com', 'myPassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    EXEC sp_OAMethod @imap, 'SelectMailbox', @success OUT, 'Inbox'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    --  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
    --  metadata describing the attachments is present, so the attachment info methods still work.
    DECLARE @headersOnly int
    SELECT @headersOnly = 1
    DECLARE @useUid int
    SELECT @useUid = 0
    DECLARE @seqNum int
    SELECT @seqNum = 1
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OAMethod @imap, 'FetchEmail', @success OUT, @headersOnly, @seqNum, @useUid, @email
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Even though the body was not downloaded, the attachment count and names are available.
    DECLARE @numAttach int
    EXEC sp_OAMethod @imap, 'GetMailNumAttach', @numAttach OUT, @email

    PRINT 'Attachments: ' + @numAttach

    IF @numAttach > 0
      BEGIN
        --  Get the attachment's original filename and use it for the saved file.
        DECLARE @fname nvarchar(4000)
        EXEC sp_OAMethod @imap, 'GetMailAttachFilename', @fname OUT, @email, 0
        EXEC sp_OAGetProperty @imap, 'LastMethodSuccess', @iTmp0 OUT
        IF @iTmp0 = 0
          BEGIN
            EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @imap
            EXEC @hr = sp_OADestroy @email
            RETURN
          END

        --  Build the output path "qa_output/<filename>".
        DECLARE @sbPath int
        EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbPath OUT

        EXEC sp_OAMethod @sbPath, 'Append', @success OUT, 'qa_output/'
        EXEC sp_OAMethod @sbPath, 'Append', @success OUT, @fname
        DECLARE @savePath nvarchar(4000)
        EXEC sp_OAMethod @sbPath, 'GetAsString', @savePath OUT

        --  Download just the first attachment (index 0) from the server and save it to that path.
        EXEC sp_OAMethod @imap, 'FetchAttachment', @success OUT, @email, 0, @savePath
        IF @success = 0
          BEGIN
            EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            EXEC @hr = sp_OADestroy @imap
            EXEC @hr = sp_OADestroy @email
            EXEC @hr = sp_OADestroy @sbPath
            RETURN
          END

        PRINT 'Saved attachment: ' + @savePath
      END

    EXEC sp_OAMethod @imap, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @email
        EXEC @hr = sp_OADestroy @sbPath
        RETURN
      END

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @email
    EXEC @hr = sp_OADestroy @sbPath


END
GO