Deleting Messages  
 

If your application needs to delete the messages in a user's mailbox, either the Post Office Protocol v3 (POP3) or Internet Message Access Protocol v4 (IMAP4) controls can be used. Which protocol is selected largely depends on the mail server. Most service providers offer POP3 access to their mail servers, and it is the more commonly used protocol.

Post Office Protocol

The Post Office Protocol is designed for mail clients which will store the messages on the local system and then delete them from the server. Here is an example of how you could store all of the messages in a user's mailbox and then delete them:

Dim nMessageId As Long
Dim strHeaders As String
Dim nError As Long

' Establish a connection to the mail server using the default port
' and the specified username and password
nError = PopClient1.Connect(strHostName, , strUserName, strPassword)
If nError > 0 Then
    MsgBox PopClient1.LastErrorString, vbExclamation
    Exit Sub
End If

' If the MessageCount property returns 0, then there are no
' messages in the mailbox
If PopClient1.MessageCount = 0 Then
    MsgBox "There are no messages in this mailbox", vbInformation
    PopClient1.Disconnect
    Exit Sub
End If
    
' Initialize the ProgressBar control
ProgressBar1.Value = 0
ProgressBar1.Max = PopClient1.LastMessage

For nMessageId = 1 To PopClient1.LastMessage
    ' Create a file name based on the message number
    strFileName = strFolder + Format(nMessageId, "00000000") + ".eml"
    
    ' Store the message in the specified file
    nError = PopClient1.StoreMessage(nMessageId, strFileName)
    If nError = 0 Then
        ' If the message was stored successfully, then delete
        ' it from the mailbox
        nError = PopClient1.DeleteMessage(nMessageId)
    End If

    ' If there was an error, warn the user
    If nError > 0 Then
        MsgBox PopClient1.LastErrorString, vbExclamation
        Exit For
    End If

    ProgressBar1.Value = nMessageId
    DoEvents
Next

PopClient1.Disconnect

Although this is very similar to the previous example that listed the available messages in the mailbox, there is an important difference. Whenever you plan on deleting messages from a POP3 mailbox, you should use the LastMessage property to determine what the message number is for the last available message in the mailbox, not the MessageCount property. Whenever a message is deleted from the POP3 mailbox, the MessageCount property value will decrease by one. This is done to reflect the fact that after message has been deleted, it can no longer be accessed. This is different than the IMAP4 protocol which merely flags a message for deletion and that message can still be accessed until the mailbox is expunged.

Internet Message Access Protocol

When you delete a message from a mailbox using the IMAP4 protocol, the message is simply flagged for deletion. Unlike the POP3 protocol, where the message can no longer be accessed, an IMAP4 client can retrieve messages that have been deleted until the mailbox has been expunged. Here is an example of how you could store all of the messages in a user's Inbox and then delete them:

Dim nMessageId As Long
Dim strSubject As String
Dim nError As Long

' Establish a connection to the mail server using the default port
' and the specified username and password
nError = ImapClient1.Connect(strHostName, , strUserName, strPassword)
If nError Then
    MsgBox ImapClient1.LastErrorString, vbExclamation
    Exit Sub
End If

' Select the user's Inbox where new messages arrive
nError = ImapClient1.SelectMailbox("Inbox")
If nError Then
    MsgBox ImapClient1.LastErrorString, vbExclamation
    Exit Sub
End If

' If the MessageCount property returns 0, then there are no
' messages in the mailbox
If ImapClient1.MessageCount = 0 Then
    MsgBox "There are no messages in this mailbox", vbInformation
    ImapClient1.Disconnect
    Exit Sub
End If
    
' Initialize the ProgressBar control
ProgressBar1.Value = 0
ProgressBar1.Max = ImapClient1.MessageCount

For nMessageId = 1 To ImapClient1.MessageCount
    ' Create a file name based on the message number
    strFileName = strFolder + Format(nMessageId, "00000000") + ".eml"
    
    ' Store the message in the specified file
    nError = ImapClient1.StoreMessage(nMessageId, strFileName)
    If nError = 0 Then
        ' If the message was stored successfully, then delete
        ' it from the mailbox
        nError = ImapClient1.DeleteMessage(nMessageId)
    End If

    ' If there was an error, warn the user
    If nError > 0 Then
        MsgBox ImapClient1.LastErrorString, vbExclamation
        Exit For
    End If

    ProgressBar1.Value = nMessageId
    DoEvents
Next

' Unselect the current mailbox, expunging the deleted messages
ImapClient1.UnselectMailbox True
ImapClient1.Disconnect

You'll notice that this code is very similar to the POP3 example, with two significant differences. The SelectMailbox method is used to explicitly select the user's Inbox, where new messages are stored until they are moved or deleted by the user. Unlike POP3 which only deals with new messages, IMAP4 is designed to manage multiple mailboxes, so you need to specify which mailbox you want to use. The mailbox "Inbox" is a special mailbox defined by the IMAP4 standard where all new messages are stored. In addition, the UnselectMailbox method is used to explicitly expunge the deleted messages from the mailbox. Note that it is possible to unselect a mailbox and leave the deleted messages intact until a later time.

Because messages are only flagged for deletion, it is possible to check for deleted messages in a mailbox by setting the Message property to the desired message number, and then checking the value of the MessageFlags property. If the imapFlagDeleted bit (a value of 512) has been set, then the message has been marked for deletion. For example:

For nMessageId = 1 To ImapClient1.MessageCount
    ImapClient1.Message = nMessageId
    If (ImapClient1.MessageFlags And imapFlagDeleted) Then
        MsgBox "Message " & nMessageId & " has been deleted", vbInformation
    End If
Next

It is also possible to undelete a message by calling the UndeleteMessage method. If you have multiple messages in a mailbox marked for deletion and you want to prevent all of them from being deleted, you can call the ReselectMailbox method which will reset the state of the current mailbox.