| 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 ProtocolThe 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:
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 ProtocolWhen 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:
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:
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. |
||
|
Copyright © 2025 Catalyst Development Corporation. All rights reserved. |