Sample code for 30+ languages & platforms
SQL Server

Append MIME from a StringBuilder with Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlagsSb method, which uploads MIME contained in a StringBuilder and sets its initial system flags. The first argument is the mailbox, the second is the StringBuilder, and the third through sixth set \Seen, \Flagged, \Answered, and \Draft. This example uploads to Drafts with the draft flag set.

Background: This is the StringBuilder form of AppendMimeWithFlags. Passing the MIME as a StringBuilder avoids copying a large message into an intermediate string and is convenient when you already built or loaded the message into a StringBuilder (for example with LoadFile).

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.AppendMimeWithFlagsSb method, which uploads MIME contained in a
    --  StringBuilder and sets its initial system flags.  The 1st argument is the mailbox, the 2nd
    --  is the StringBuilder, and the 3rd through 6th set \Seen, \Flagged, \Answered, and \Draft.

    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

    --  Build the email to be uploaded.
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    EXEC sp_OASetProperty @email, 'Subject', 'Meeting agenda'
    EXEC sp_OASetProperty @email, 'From', 'Alice <alice@example.com>'
    EXEC sp_OAMethod @email, 'AddTo', @success OUT, 'Bob', 'bob@example.com'
    EXEC sp_OASetProperty @email, 'Body', 'Let''s meet at 10am to review the agenda.'

    DECLARE @mimeText nvarchar(4000)
    EXEC sp_OAMethod @email, 'GetMime', @mimeText OUT
    EXEC sp_OAGetProperty @email, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @imap
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Load the MIME into a StringBuilder.  (A StringBuilder can also be filled via LoadFile.)
    DECLARE @sbMime int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbMime OUT

    EXEC sp_OAMethod @sbMime, 'Append', @success OUT, @mimeText

    --  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    DECLARE @seen int
    SELECT @seen = 0
    DECLARE @flagged int
    SELECT @flagged = 0
    DECLARE @answered int
    SELECT @answered = 0
    DECLARE @draft int
    SELECT @draft = 1

    --  Upload to "Drafts" with the \Draft flag set.
    EXEC sp_OAMethod @imap, 'AppendMimeWithFlagsSb', @success OUT, 'Drafts', @sbMime, @seen, @flagged, @answered, @draft
    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 @sbMime
        RETURN
      END


    EXEC sp_OAGetProperty @imap, 'AppendUid', @iTmp0 OUT
    PRINT 'Appended UID: ' + @iTmp0

    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 @sbMime
        RETURN
      END

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


END
GO