Usuwaj pojedyncze i wiele wiadomości e‑mail przy użyciu ImapClient

Usuwanie wiadomości

Ten ImapClient klasa może usuwać wiadomości z serwera IMAP. The ImapClient klasa DeleteMessage() funkcja służy do usuwania wiadomości. Przyjmuje jako parametr numer kolejny wiadomości lub unikalny identyfikator. The ImapClient udostępnia DeleteMessage i DeleteMessages metody usuwania wiadomości pojedynczo lub wielokrotnie. Poniższy fragment kodu pokazuje, jak usunąć wiadomość e-mail o identyfikatorze 1 z serwera IMAP.

using var client = new ImapClient("host", "username", "password");
client.SecurityOptions = SecurityOptions.SSLImplicit;

// Append test message
client.SelectFolder(ImapFolderInfo.InBox);

var eml = new MailMessage("from@from.com", "to@to.com")
{
  Subject = "Message to delete",
  Body = "Hey! This Message will be deleted!"
};
var emlId = client.AppendMessage(eml);

// Delete appended message
client.DeleteMessage(emlId);
client.CommitDeletes();

Usuwanie wielu wiadomości

Wiele wiadomości e-mail można usunąć z skrzynki pocztowej przy użyciu ImapClient of Aspose.Email API. The DeleteMessages metoda zapewnia szereg opcji usuwania wielu wiadomości z serwera przy użyciu unikalnych identyfikatorów, numerów sekwencyjnych lub ImapMessageInfoCollection elementy. Poniższy fragment kodu pokazuje, jak usunąć wiele wiadomości.

using var client = new ImapClient("host", "username", "password");
client.SelectFolder(ImapFolderInfo.InBox);
            
// Append test messages
var emlList = new List<MailMessage>();
{
  var eml = new MailMessage("from@from.com", "to@to.com")
  {
    Subject = $"Message to delete {i}",
    Body = "Hey! This Message will be deleted!"
  };
                
  emlList.Add(eml);
}

var appendMessagesResult = client.AppendMessages(emlList);
            
// Bulk Delete appended Messages
client.DeleteMessages(appendMessagesResult.Succeeded.Values, true);
client.CommitDeletes();