SQL Server Stored Procedure Examples

ChilkatHOMEAndroid™ASPVisual BasicVB.NETC#iOS (IPhone)Objective-CC++CMFCDelphiFoxProJavaPerl
PHP ExtensionPHP ActiveXPythonPowerShellRubySQL ServerVBScript

SQL Server
Stored Procedure Examples

Quick Start
Encryption
File Access
IMAP
POP3
SMTP
Email Object
DKIM / DomainKey
FTP
HTML Conversion
HTTP
MHT
MIME
NTLM
RSA
Diffie-Hellman
DSA
Socket
Spider
SSH Key
SSH
SSH Tunnel
SFTP
String
Tar
Upload
XML
XMP
Zip

Amazon S3
Bz2
CSV
FileAccess
Byte Array
RSS
Atom
Self-Extractor

Process Large POP3 Mailbox

Demonstrates how to read email from a mailbox that may contain a large number of emails (on the order of thousands of emails or more).

Download Chilkat Email ActiveX for POP3 / SMTP

CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    --  The mailman object is used for receiving (POP3)
    --  and sending (SMTP) email.
    DECLARE @mailman int
    EXEC @hr = sp_OACreate 'Chilkat.MailMan2', @mailman OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Any string argument automatically begins the 30-day trial.
    DECLARE @success int

    EXEC sp_OAMethod @mailman, 'UnlockComponent', @success OUT, '30-day trial'
    IF @success <> 1
      BEGIN
        PRINT 'Component unlock failed'
        RETURN
      END

    --  Set the POP3 server's hostname
    EXEC sp_OASetProperty @mailman, 'MailHost', 'mail.chilkatsoft.com'

    --  Set the POP3 login/password.
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'myLogin'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  First, get the list of UIDLs for all emails in the mailbox.
    DECLARE @sa int

    EXEC sp_OAMethod @mailman, 'GetUidls', @sa OUT
    DECLARE @i int

    DECLARE @numEmails int

    EXEC sp_OAGetProperty @sa, 'Count', @numEmails OUT

    --  Download the emails in chunks of 10 emails each.
    DECLARE @chunkBeginIdx int

    SELECT @chunkBeginIdx = 0
    DECLARE @chunkEndIdx int

    SELECT @chunkEndIdx = 9
    IF @chunkEndIdx >= @numEmails
      BEGIN
        SELECT @chunkEndIdx = @numEmails - 1
      END

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

    WHILE (@chunkEndIdx < (@numEmails - 1))
      BEGIN

        --  Build a chunk of 10 UIDLs.
        EXEC sp_OAMethod @saChunk, 'Clear', NULL
        SELECT @i = @chunkBeginIdx
        WHILE @i <= @chunkEndIdx
          BEGIN
            EXEC sp_OAMethod @sa, 'GetString', @sTmp0 OUT, @i            EXEC sp_OAMethod @saChunk, 'Append', NULL, @sTmp0
            SELECT @i = @i + 1
          END

        --  Display the UIDLs in this chunk...
        DECLARE @chunkStr nvarchar(4000)

        EXEC sp_OAMethod @saChunk, 'SaveToText', @chunkStr OUT

        PRINT @chunkStr


        PRINT '----' + CHAR(13)+CHAR(10)

        --  Download this chunk of email from the POP3 server.
        DECLARE @bundle int

        EXEC sp_OAMethod @mailman, 'FetchMultiple', @bundle OUT, @saChunk
        IF @bundle Is NULL 
          BEGIN
            EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
            PRINT @sTmp0
            RETURN
          END

        --  Process the bundle...
        --  (your application's processing code goes here...)

        --  Get the next chunk...
        SELECT @chunkBeginIdx += 10
        IF @chunkBeginIdx >= @numEmails
          BEGIN
            BREAK
          END
        SELECT @chunkEndIdx += 10
        IF @chunkEndIdx >= @numEmails
          BEGIN
            SELECT @chunkEndIdx = @numEmails - 1
          END

      END


END
GO

 

© 2000-2010 Chilkat Software, Inc. All Rights Reserved.