Sample code for 30+ languages & platforms
SQL Server

Fetch a Range of POP3 Messages

See more POP3 Examples

Demonstrates the Chilkat MailMan.FetchRange method, which retrieves a range of messages from the POP3 mailbox and appends them to an EmailBundle. The arguments are keepOnServer, headersOnly, numBodyLines, a zero-based inclusive startIndex and endIndex, and the EmailBundle. This example fetches the first five messages.

Background: Fetching a range instead of everything is useful for paging through a large mailbox, or resuming after an interruption — download messages 0–99, then 100–199, and so on. Combined with headersOnly, it lets you build a quick message-list view without pulling full bodies and attachments up front.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Demonstrates the MailMan.FetchRange method, which retrieves a range of messages from the
    --  POP3 mailbox and appends them to an EmailBundle.  The arguments are keepOnServer,
    --  headersOnly, numBodyLines, startIndex, endIndex, and the EmailBundle.  The indexes are
    --  zero-based and inclusive.

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

    --  Configure the POP3 server connection.
    EXEC sp_OASetProperty @mailman, 'MailHost', 'pop.example.com'
    EXEC sp_OASetProperty @mailman, 'MailPort', 995
    EXEC sp_OASetProperty @mailman, 'PopSsl', 1
    EXEC sp_OASetProperty @mailman, 'PopUsername', 'user@example.com'
    EXEC sp_OASetProperty @mailman, 'PopPassword', 'myPassword'

    --  Fetch the first 5 messages (indexes 0 through 4), full bodies, leaving them on the server.
    --  keepOnServer=1, headersOnly=0 (fetch the entire message).
    --  Note: numBodyLines only applies when headersOnly is 1 (it sets how many lines of the
    --  body to include along with the headers).  When headersOnly is 0, the entire message
    --  is fetched and numBodyLines is ignored.
    DECLARE @bundle int
    EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @bundle OUT

    EXEC sp_OAMethod @mailman, 'FetchRange', @success OUT, 1, 0, 0, 0, 4, @bundle
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @bundle
        RETURN
      END

    DECLARE @n int
    EXEC sp_OAGetProperty @bundle, 'MessageCount', @n OUT


    PRINT 'Fetched ' + @n + ' messages.'

    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

    DECLARE @i int

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @bundle, 'EmailAt', @success OUT, @i, @email
        EXEC sp_OAGetProperty @email, 'Subject', @sTmp0 OUT
        PRINT @sTmp0
        SELECT @i = @i + 1
      END

    --  Note: Explicitly connecting/authenticating is optional.  Chilkat MailMan automatically
    --  connects and authenticates -- using the property settings above -- whenever a server
    --  operation requires it.  Calling the explicit connect/authenticate methods can still be
    --  helpful to determine whether a failure occurs while connecting or while authenticating.

    EXEC @hr = sp_OADestroy @mailman
    EXEC @hr = sp_OADestroy @bundle
    EXEC @hr = sp_OADestroy @email


END
GO