ImapExpunge Method (String, Boolean) |
Namespace: MailBee.ImapMail
Exception | Condition |
---|---|
MailBeeProtocolExtensionNotSupportedException | UID EXPUNGE required but not supported by the server and ThrowExceptions is true. |
MailBeeException | An error occurred and ThrowExceptions is true. |
This overload can be used to expunge the specified messages only. Often, it's required to remove certain message from the folder but leave other messages marked as deleted intact. The standard IMAP4 protocol does not allow this to be done but if the server supports UIDPLUS extension, it becomes possible (via UID EXPUNGE command).
This method always issues EXPUNGE if uidSet is not set. If uidSet is not a null reference, the method will attempt to delete the messages having UIDs from uidSet list and \Deleted flag set. If, however, UIDPLUS is not supported, the method will either simulate effect of UID EXPUNGE using a number of regular IMAP4 commands (if forceUidExpunge is false) or report error and do not expunge any messages (if forceUidExpunge is true).
In most cases, it's ok to set forceUidExpunge to false. The only reason for not doing so is when the component is used with the IMAP folder where messages are added very often by another clients. In this case, while the component executes a sequence of commands to expunge certain messages, another mail client can add, remove, or manage messages in the same folder. In this case, the folder state may go out of sync. In all other cases, if no other mail agents access the same folder at the same time, it's safe to use MailBee simulation of UID EXPUNGE command when it's not supported by the server.
using System; using MailBee; using MailBee.ImapMail; class Sample { static void Main(string[] args) { Imap imp = new Imap(); // Connect to the server and log in the account. imp.Connect("imap.domain.com"); imp.Login("jdoe@domain.com", "secret"); // Select Inbox folder. imp.SelectFolder("INBOX"); // Mark message #1 as deleted. imp.SetMessageFlags("1", false, SystemMessageFlags.Deleted, MessageFlagAction.Add); // To expunge specific message, we need to know its UID. EnvelopeCollection uidList = imp.DownloadEnvelopes("1", false, EnvelopeParts.Uid, 0); if (uidList.Count > 0) { // Expunge message #1 by its UID. imp.Expunge(uidList[0].Uid.ToString(), false); } // We do not call Close() here to prevent remaining messages // marked as deleted from being expunged. imp.Disconnect(); } }