Sample code for 30+ languages & platforms
SQL Server

Delete a Bundle of POP3 Messages

See more POP3 Examples

Demonstrates the Chilkat MailMan.DeleteBundle method, which deletes POP3 messages using the UIDLs stored in the X-UIDL headers of the Email objects in an EmailBundle. An email without an X-UIDL header is skipped. This example fetches all messages and deletes them.

Background: DeleteBundle is the batch version of DeleteEmail: it marks every message in a downloaded bundle for deletion in one call. A typical "download and clear the mailbox" workflow is to fetch all messages, process them, then delete the whole bundle. Because a bundle can be filtered or built selectively, you can also delete just the subset you no longer need. The deletions commit when the POP3 session ends.

Chilkat SQL Server Downloads

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

    --  Demonstrates the MailMan.DeleteBundle method, which deletes POP3 messages using the UIDLs
    --  stored in the X-UIDL headers of the Email objects in an EmailBundle.  An email without an
    --  X-UIDL header is skipped.

    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 all messages (headers are enough; each carries an X-UIDL header).
    DECLARE @bundle int
    EXEC @hr = sp_OACreate 'Chilkat.EmailBundle', @bundle OUT

    EXEC sp_OAMethod @mailman, 'FetchAll', @success OUT, 1, 1, 0, @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

    --  Delete every message in the bundle.
    EXEC sp_OAMethod @mailman, 'DeleteBundle', @success OUT, @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

    --  Unless the ImmediateDelete property is set to 1, the messages are only marked for
    --  deletion.  End the POP3 session (which sends the QUIT command) to commit the deletions.
    EXEC sp_OAMethod @mailman, 'Pop3EndSession', @success OUT
    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


    EXEC sp_OAGetProperty @bundle, 'MessageCount', @iTmp0 OUT

    PRINT 'Deleted ' + @iTmp0 + ' messages from the POP3 server.'

    --  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


END
GO