Sample code for 30+ languages & platforms
SQL Server

Upload .eml File to an IMAP Mailbox

See more IMAP Examples

Demonstrates how to upload the MIME source of an email to a mailbox on an IMAP 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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

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

    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    -- Connect to an IMAP server.
    -- Use TLS
    EXEC sp_OASetProperty @imap, 'Ssl', 1
    EXEC sp_OASetProperty @imap, 'Port', 993
    EXEC sp_OAMethod @imap, 'Connect', @success OUT, 'MY-IMAP-DOMAIN'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    -- Login
    EXEC sp_OAMethod @imap, 'Login', @success OUT, 'MY-IMAP-LOGIN', 'MY-IMAP-PASSWORD'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        RETURN
      END

    DECLARE @sbMime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT

    EXEC sp_OAMethod @sbMime, 'LoadFile', @success OUT, 'qa_data/eml/emoji_pizza.eml', 'utf-8'

    -- Upload to the mailbox.
    EXEC sp_OAMethod @sbMime, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @imap, 'AppendMime', @success OUT, '[Gmail]/testFolder', @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @imap, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @sbMime
        RETURN
      END

    EXEC sp_OAMethod @imap, 'Disconnect', @success OUT


    PRINT 'OK.'

    EXEC @hr = sp_OADestroy @imap
    EXEC @hr = sp_OADestroy @sbMime


END
GO