Usuwanie e‑maili z serwera IMAP

Usuwanie pojedynczej wiadomości z serwera

Aby usunąć wiadomość z serwera IMAP, biblioteka Aspose.Email udostępnia metodę delete_message klasy ImapClient klasa. Przyjmuje numer sekwencyjny wiadomości lub unikalny identyfikator jako parametr. Poniższy fragment kodu pokazuje, jak usunąć wiadomość e‑mail o ID 1 z serwera IMAP:

import aspose.email as ae
from aspose.email.clients.imap import ImapClient

with ImapClient("imap.domain.com", 993, "username", "password",
                ae.clients.SecurityOptions.AUTO) as client:
    client.select_folder("Inbox")

    message = ae.MailMessage("from@domain.com", "to@domain.com",
                             "Message deletion using the IMAP client",
                             "This message will be deleted")

    print("Total messages in the Inbox before appending:", len(client.list_messages()))

    email_id = client.append_message(message)
    print("The message was appended to the Inbox with the id:", email_id)

    print("Total messages in the Inbox after appending:", len(client.list_messages()))

    client.delete_message(email_id)
    client.commit_deletes()

    print("Total messages in the Inbox after deletion:", len(client.list_messages()))

Usuwanie wielu wiadomości z serwera

Wiele wiadomości e‑mail może być usuniętych z skrzynki przy użyciu metody delete_messages klasy ImapClient klasa udostępniana przez API Aspose.Email. Metoda ta oferuje wiele opcji usuwania wielu wiadomości z serwera przy użyciu unikalnych identyfikatorów, numerów sekwencyjnych lub elementów ImapMessageInfoCollection. Poniższy fragment kodu pokazuje, jak usunąć wiele wiadomości:

import aspose.email as ae
from aspose.email.clients.imap import ImapClient

with ImapClient("imap.domain.com", 993, "username", "password",
                ae.clients.SecurityOptions.AUTO) as client:
    client.select_folder("Inbox")

    print("Total messages in the Inbox before appending:", len(client.list_messages()))

    # Append two test messages and collect their ids
    uid_list = []
    for number in (1, 2):
        message = ae.MailMessage("from@domain.com", "to@domain.com",
                                 f"Message {number}", "This message will be deleted")
        uid_list.append(client.append_message(message))

    print("Total messages in the Inbox after appending:", len(client.list_messages()))

    # The second argument tells the client to treat the ids as unique ids
    client.delete_messages(uid_list, True)
    client.commit_deletes()

    print("Total messages in the Inbox after deletion:", len(client.list_messages()))