Xóa email từ máy chủ IMAP

Xóa một tin nhắn duy nhất khỏi máy chủ

Để xóa một tin nhắn từ máy chủ IMAP, thư viện Aspose.Email cung cấp phương thức delete_message của ImapClient lớp. Nó nhận số thứ tự tin nhắn hoặc ID duy nhất làm tham số. Đoạn mã sau cho bạn thấy cách xóa một tin nhắn email có ID 1 từ máy chủ 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()))

Xóa nhiều tin nhắn khỏi máy chủ

Nhiều email có thể được xóa khỏi hộp thư bằng phương thức delete_messages của ImapClient lớp được cung cấp bởi API Aspose.Email. Phương pháp này cung cấp nhiều tùy chọn để xóa nhiều tin nhắn khỏi máy chủ bằng cách sử dụng ID duy nhất, số thứ tự hoặc các phần tử ImapMessageInfoCollection. Đoạn mã sau cho bạn thấy cách xóa nhiều tin nhắn:

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()))