4
votes

I have been using java mail (POP3 client & IMAP) to automate Gmail operation. One of the operation is to delete mail and I use 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();
        }
    }

The Mail is deleted from "Inbox" but its available in "All Mail" only not in "Trash" folder. I want to delete it permanently. Is there any straight way to delete mail permanently instead of deleting mail from "inbox"?

3

3 Answers

4
votes

Unfortunately, it is a multi-step process, and needs to be done with IMAP, not POP. To permanently delete something from GMail, it appears you need to move it to the Trash, then delete it from the trash. It supports the MOVE extension so this is not too bad. You will also have to discover the Trash folder name, perhaps 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 be truly gone.

For most users, moving it to the Trash for them is sufficient. It will disappear on its own later.

0
votes

Probably you will solve this issue in your Gmail Account settings.

Gmail has the following options at Settings page, Forwarding and POP/IMP tab:

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

  • Archive the message (default)
  • Move the message to Trash
  • Immediately delete the message forever

Try set the last option (Immediately delete the message forever) in your account and run your code again.

Good luck.

0
votes

Follow these steps to permanently delete the messages from Gmail using JavaMail api

  1. Connect to Store
  2. Open Folder Inbox in Read/Write mode.
  3. Open Folder Bin in Read/Write mode.
  4. Get Messages from Inbox.
  5. Copy Messages from Inbox to Bin.
  6. Set flag for all messages as DELETED.
  7. Expunge the folder.
  8. Close the Store

    def deleteMessages(String userName, String password) {
    
          Properties properties = new Properties();
    
          String host = "imap.gmail.com";
          String port = "993";
          String mailStoreType = "pop3";
    
          // server setting
          properties.put("mail.imap.host", host);
          properties.put("mail.imap.port", port);
    
          // SSL setting
          properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
          properties.setProperty("mail.imap.socketFactory.fallback", "false");
          properties.setProperty("mail.imap.socketFactory.port", String.valueOf(port));
    
          Session session = Session.getDefaultInstance(properties);
    
          try {
              // connects to the message store
              Store store = session.getStore("imap");
              store.connect(userName, password);
    
              // opens the inbox folder
              Folder folderInbox = store.getFolder("INBOX");
              folderInbox.open(Folder.READ_WRITE);
    
              //opens the trash folder
              Folder folderBin = store.getFolder("[Gmail]/Bin");
              folderBin.open(Folder.READ_WRITE);
    
              // fetches new messages from server
              Message[] arrayMessages = folderInbox.getMessages();
    
              //Copy messages from inbox to Trash
              folderInbox.copyMessages(arrayMessages, folderBin)
    
              arrayMessages = folderBin.getMessages();
    
              for (int i = 0; i < arrayMessages.length; i++) {
                     Message message = arrayMessages[i];
                     String subject = message.getSubject();
                     message.setFlag(Flags.Flag.DELETED, true);
    
              }
    
              // expunges the folder to remove messages which are marked deleted
              boolean expunge = true;
              folderBin.close(expunge);
              folderInbox.close(expunge);
    
              // disconnect
              store.close();
          } catch (NoSuchProviderException ex) {
    
              System.out.println("No provider.");
              ex.printStackTrace();
          } catch (FolderNotFoundException ex) {
    
              System.out.println("Folder Not Found")
              ex.printStackTrace();
          } catch (MessagingException ex) {
    
               System.out.println("Could not connect to the message store.");
               ex.printStackTrace();
          }
    }