How to Delete Mail in Gmail Using Java Mail (POP3 Client & IMAP)

I am using java mail ( POP3 client and IMAP ) to automate Gmail. One of the operations is to delete mail and I am using the following code -

public void deleteInboxMasseges() throws IOException, MessagingException
{
    store = getConnection(EMAIL_USERNAME, EMAIL_PASSWORD);
        if (store != null)
        {
            int inboxMassegeCount = inbox.getMessageCount();
            Message[] messages = inbox.getMessages();
            for (int i = 0; i < inboxMassegeCount; i++)
            {
                messages[i].setFlag(Flags.Flag.DELETED, true);
            }
            inbox.expunge();
        }
    }

      

Mail is removed from Inbox , but it is only available in All Mail in the Trash folder . I want to delete it permanently. Is there a direct way to delete mail permanently instead of deleting mail from your Inbox ?

+3


source to share


2 answers


Unfortunately, this is a multi-step process and needs to be done using IMAP, not POP. To permanently remove something from GMail, you need to move it to the trash and then remove it from the trash. It supports the MOVE extension, so it's not that bad. You will also need to open the Trash folder name, possibly using LIST as it is localized.

a001 UID MOVE xxx "[Gmail]/Trash"
* OK [MOVEUID xxxxxxx yyy] Message moved
a002 SELECT "[Gmail]/Trash"
a003 UID STORE yyy +FLAGS (\Deleted)
a004 UID EXPUNGE yyy

      



Then it will really go.

For most users, moving it to the trash can is enough for them. It will disappear on its own later.

+3


source


You can probably fix this problem in your Gmail account settings.

Gmail has the following options on the Options page, Forwarding page, and the POP / IMP tab:

When a message is marked as deleted and removed from the last visible IMAP folder:



  • Archive message (default)
  • Move message to trash
  • Immediately delete the message permanently

Try installing the last option (Immediately delete post permanently) in your account and run your code again.

Good luck.

0


source







All Articles