Delete Mail using message id

I build my mail server using the MailInABox and now I am building an email client. I want to make a functionality of delete a particular email from the trash folder. I want the mail is permanently deleted from the mail server. This is my code-

imap.once(“ready”, () => {
imap.openBox(“Trash”, true, (err, box) => {
if (err) throw err;

  // Search for the email(s) you want to delete (e.g., by message ID)
  const messageId = "message-id";
  imap.search(
    [["HEADER", "MESSAGE-ID", messageId]],
    (err, searchResults) => {
      if (err) throw err;

      // Check if any emails are found
      if (searchResults.length === 0) {
  
        imap.end();
        return;
      }

      imap.addFlags(searchResults, "\\Deleted", (err) => {
        if (err) throw err;
        imap.expunge(searchResults,(err) => {
          if (err) throw err;

          console.log("Email(s) deleted permanently");
          imap.end();
        });
      });
    }
  );
});

});

imap.once(“error”, (err) => {
console.error(err);
});

imap.connect();

Okay, what do you exactly want? Do you got an error? Is it not working?

This topic was automatically closed 40 days after the last reply. New replies are no longer allowed.