Sample code for 30+ languages & platforms
SQL Server

Delete a POP3 Message via an Email Object

See more POP3 Examples

Demonstrates the Chilkat MailMan.DeleteEmail method, which deletes a POP3 message using the UIDL stored in the X-UIDL header of an Email object. The email must contain an X-UIDL header, which it does after being fetched from the server. This example fetches a message and then deletes it.

Background: When Chilkat downloads a POP3 message it records the server UIDL in an X-UIDL header on the Email object. DeleteEmail uses that header to tell the server which message to remove — a natural "process then delete" flow: fetch a message, act on it, then delete it. As always in POP3, the deletion is only committed when the session ends (unless ImmediateDelete is set).

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.DeleteEmail method, which deletes a POP3 message using the UIDL
    --  stored in the X-UIDL header of an Email object.  The email must contain an X-UIDL header
    --  (as it does after being fetched from the server).

    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 a message (headers are enough; the X-UIDL header identifies it on the server).
    DECLARE @email int
    EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT

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

    --  Delete that message using its X-UIDL header.
    EXEC sp_OAMethod @mailman, 'DeleteEmail', @success OUT, @email
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @mailman, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @mailman
        EXEC @hr = sp_OADestroy @email
        RETURN
      END

    --  Unless the ImmediateDelete property is set to 1, the message is only marked for
    --  deletion.  End the POP3 session (which sends the QUIT command) to commit the deletion.
    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 @email
        RETURN
      END


    PRINT 'Deleted the email 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 @email


END
GO