Sample code for 30+ languages & platforms
SQL Server

Fetch a Text IMAP Attachment as a String

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachmentString method, which returns one text attachment decoded to a string. The first argument is the Email, the second is the zero-based attachment index, and the third is the charset used to decode the bytes. This example returns the first attachment as a UTF-8 string.

Background: This is the convenient form when you simply want a small text attachment's content inline as a string. Only use it for attachments known to be text; decoding binary content through a charset can alter or corrupt the bytes. For binary data use FetchAttachmentBd, and for large text a StringBuilder (via FetchAttachmentSb) avoids an extra copy.

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.FetchAttachmentString method, which downloads one text attachment and
    --  returns it decoded to a string.  The 1st argument is the Email, the 2nd is the zero-based
    --  attachment index, and the 3rd is the charset used to decode the bytes.
    --  
    --  The message is fetched headers-only, then the attachment is downloaded on demand.

    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

    DECLARE @numAttach int
    EXEC sp_OAMethod @imap, 'GetMailNumAttach', @numAttach OUT, @email
    IF @numAttach > 0
      BEGIN
        --  FetchAttachmentString returns a string, so assign it to a string variable.
        DECLARE @attachText nvarchar(4000)
        EXEC sp_OAMethod @imap, 'FetchAttachmentString', @attachText OUT, @email, 0, 'utf-8'
        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

        PRINT @attachText
      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
        RETURN
      END

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


END
GO