Because mailboxes are frequently limited in size by the mail system administrator, it is common practice to delete those messages which have been stored on the local system or have been marked for deletion by the client. To delete a message from the mailbox, use the DeleteMessage method. This will mark the message for deletion, removing it from the mailbox when the client session is terminated. Once a message has been marked for deletion it is no longer available to the client and attempting to access the message (for example, by setting the MessageIndex property) will result in an error.
As messages are marked for deletion from the mailbox, the MessageCount property will be decremented to reflect the current number of messages. Because of this, the MessageCount property should generally not be used in constructs like For..Next or Do..While loops where each message is being processed. Instead, use the LastMessage property which remains constant for the duration of the client session. To delete all of the messages in the mailbox, use code like this:
For nMessage = 1 To InternetMail1.LastMessage
If Not InternetMail1.DeleteMessage(nMessage) Then
MsgBox(InternetMail1.LastErrorString, MsgBoxStyle.Exclamation)
Exit Sub
End If
Next
To prevent those messages from actually being removed from the mailbox, call the Reset method which resets both the state of the class instance and the current mailbox (if a connection to a mail server has been established). Keep in mind that once the Disconnect method is called, any messages that have been marked for deletion are permanently removed from the mailbox and cannot be recovered.