Deleting Messages from Server

Deleting Messages

The ImapClient class can delete messages from an IMAP server. The ImapClient class deleteMessage() function is used to delete messages. It takes the message sequence number or unique ID as a parameter. The ImapClient provides deleteMessage and deleteMessages methods for deleting messages one by one or multiple. The following code snippet shows you how to delete an email message with the message ID 1 from an IMAP server.

try (ImapClient client = new ImapClient("host", "username", "password")) {
    client.setSecurityOptions(SecurityOptions.SSLImplicit);

    // Append test message
    client.selectFolder(ImapFolderInfo.IN_BOX);

    MailMessage eml = new MailMessage("from@from.com", "to@to.com");
    eml.setSubject("Message to delete");
    eml.setBody("Hey! This Message will be deleted!");
    String emlId = client.appendMessage(eml);

    // Delete appended message
    client.deleteMessage(emlId);
    client.commitDeletes();
}

Deleting Multiple Messages

Multiple emails can be deleted from mailbox using the ImapClient of Aspose.Email API. The deleteMessages method provides a number of options to delete multiple messages from the server using unique ids, sequence numbers or ImapMessageInfoCollection elements. The following code snippet shows you how to delete multiple messages.

try (ImapClient client = new ImapClient("host", "username", "password")) {
    client.selectFolder(ImapFolderInfo.IN_BOX);

    // Append test messages
    List<MailMessage> emlList = new ArrayList<>();
    for (int i = 0; i < 3; i++) {
        MailMessage eml = new MailMessage("from@from.com", "to@to.com");
        eml.setSubject("Message to delete " + i);
        eml.setBody("Hey! This Message will be deleted!");

        emlList.add(eml);
    }

    AppendMessagesResult appendMessagesResult = client.appendMessages(emlList);
    List<String> uidList = new ArrayList<>();
    for (String uid : appendMessagesResult.getSucceeded().getValues()) {
        uidList.add(uid);
    }

    // Bulk Delete appended Messages
    client.deleteMessagesByUids(uidList, true);
    client.commitDeletes();
}