Deleting & expunging messages

This sample connects to IMAP4 server, logs in e-mail account, selects "Inbox" folder and permanently removes all messages which were already seen before.

Search method with "SEEN" search condition is used to locate already seen messages.

DeleteMessages method is used to mark messages for deletion.

ExpungeAndClose method is used to close "Inbox" mailbox and permanently delete messages marked for deletion (you can also use Expunge method to purge messages without closing the mailbox).

Visual Basic

Dim objIMAP4, arrSeen, I

Set objIMAP4 = CreateObject("MailBee.IMAP4")

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

' Set user credentials
objIMAP4.UserName = "username"
objIMAP4.Password = "password"

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Get message numbers of already seen messages
    arrSeen = objIMAP4.Search(False, "SEEN")
    
    If Not objIMAP4.IsError Then
      ' Loop through already seen messages
      For I = LBound(arrSeen) To UBound(arrSeen)
        ' Mark message for deletion
        objIMAP4.DeleteMessages arrSeen(I), arrSeen(I), False
      Next
    Else
      ' Display error information
      MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If

    ' Purge deleted messages and close "Inbox"
    If Not objIMAP4.ExpungeAndClose Then
      ' On error, no emails would be purged:
      ' display error information
      MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    MsgBox "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  MsgBox "Error #" & objIMAP4.ErrCode
  MsgBox "Server response: " & objIMAP4.ServerResponse
End If

ASP

<%
Dim objIMAP4, arrSeen, I

Set objIMAP4 = Server.CreateObject("MailBee.IMAP4")

' Unlock IMAP4 object
objIMAP4.LicenseKey = "put your license key here"

' Set IMAP4 server name
objIMAP4.ServerName = "mail.server.com"

' Set user credentials
objIMAP4.UserName = "username"
objIMAP4.Password = "password"

' Connect to the server and
' log in email account
If objIMAP4.Connect Then

  ' Select Inbox folder
  If objIMAP4.SelectMailbox("Inbox") Then

    ' Get message numbers of already seen messages
    arrSeen = objIMAP4.Search(False, "SEEN")
    
    If Not objIMAP4.IsError Then
      ' Loop through already seen messages
      For I = LBound(arrSeen) To UBound(arrSeen)
        ' Mark message for deletion
        objIMAP4.DeleteMessages arrSeen(I), arrSeen(I), False
      Next
    Else
      ' Display error information
      Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc & "<br>"
    End If

    ' Purge deleted messages and close "Inbox"
    If Not objIMAP4.ExpungeAndClose Then
      ' On error, no emails would be purged:
      ' display error information
      Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
    End If
  Else
    ' Display error information
    Response.Write "Error #" & objIMAP4.ErrCode & ", " & objIMAP4.ErrDesc
  End If

  ' Close the connection
  objIMAP4.Disconnect
Else
  ' Display error information
  Response.Write "Error #" & objIMAP4.ErrCode & "<br>"
  Response.Write "Server response: " & objIMAP4.ServerResponse
End If
%>

See Also:

DeleteMessages Method

ExpungeAndClose Method