1

I have written java code using java mail API. Which does the following things for IMAPs account:

  1. Mark mail as read/Unread.
  2. Send/Delete mails.

My code works well for most of IMAP mail accounts. I am fetching the mails based on their UID. But surprisingly it isn't working for the delete operation for the mail accounts having domain xxx12@yahoo.co.jp. I also tried creating a new account having @yahoo.co.jp in the domain.

The things which are working for mail accounts having @yahoo.co.jp are:

  1. Mark as read/Unread.

So far i have tried this link Delete Email on Server using javax.mail and read https://javaee.github.io/javamail/FAQ . I have also tried delete operation with other yahoo mails(i.e xxx12@yahoo.com) along with gmail mail account in which the below code works as expected, both the types that were tested were of IMAP type.

Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
props.put("mail.smtp.starttls.enable", true);
session = Session.getInstance(props, null);
session.setDebug(true);
store = session.getStore("imaps");
store.connect("host", "port","uname","pass");
inbox = (IMAPFolder) store.getFolder(folderName);
inbox.open(Folder.READ_WRITE);
long uid = Long.parseLong(messageId);
message = inbox.getMessageByUID(uid);
if (message != null) {
    if(form.getFlagType().equals("read")){
        Flags read = new Flags(Flags.Flag.SEEN);
        message.setFlags(read, true);
    } else if(form.getFlagType().equals("unread")){
        Flags unread = new Flags(Flags.Flag.SEEN);
        message.setFlags(unread, false);
    } else if(form.getFlagType().equals("delete")){
        Flags deleteMail = new Flags(Flags.Flag.DELETED);
        message.setFlags(deleteMail, true);
    }
}
inbox.close(true); 
catch(Exception e){
    e.printStackTrace();
}

The line written in above code message.setFlags(deleteMail, true);

Gives an exception as show below:

javax.mail.MessagingException:

A4 NO [CANNOT] STORE It's not possible to perform specified operation;

nested exception is:

com.sun.mail.iap.CommandFailedException: A4 NO [CANNOT] STORE It's not possible to perform specified operation

I want to delete mails having yahoo.co.jp as their domain. Any help would be greatly appreciated.

UPDATE:

After adding the session.setDebug(true);

I found the following:

A2 SELECT INBOX
* 3 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1557837307] UIDs valid
* OK [UIDNEXT 20006] Predicted next UID
* FLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $Junk $NotJunk)
* OK [PERMANENTFLAGS (\Answered \Deleted \Draft \Flagged \Seen $Forwarded $Junk $NotJunk)] Permanent flags
* OK [HIGHESTMODSEQ 27]
A2 OK [READ-WRITE] SELECT completed; now in selected state
A3 UID FETCH 20005 (UID)
* 3 FETCH (UID 20005)
A3 OK UID FETCH completed
A4 STORE 3 +FLAGS (\Deleted)
A4 NO [CANNOT] STORE It's not possible to perform specified operation
DEBUG IMAPS: IMAPProtocol noop
A5 NOOP
A5 OK NOOP completed
A6 CLOSE
A6 OK CLOSE completed
DEBUG IMAPS: added an Authenticated connection -- size: 1
DEBUG IMAPS: IMAPProtocol noop
A7 NOOP
A7 OK NOOP completed
A8 LOGOUT
* BYE IMAP4rev1 Server logging out
A8 OK LOGOUT completed
DEBUG IMAPS: IMAPStore connection dead
DEBUG IMAPS: IMAPStore cleanup, force false
DEBUG IMAPS: IMAPStore cleanup done

Please ask if you need any further details on the question.

Jennis Vaishnav
  • 331
  • 7
  • 29
  • What is "the specified operation"? Some IMAP debug logs would help here. – arnt May 15 '19 at 12:24
  • @arnt I have updated the question please check. I already tried adding a debug but sadly didn't find anything useful :( . – Jennis Vaishnav May 15 '19 at 12:46
  • It does rather sound as if you have read-only access. The earlier part of the debug log would tell, most importantly the response to `SELECT`. – arnt May 15 '19 at 12:48
  • @arnt I have updated the full log from mail being opened to inbox.close. Hope this helps. – Jennis Vaishnav May 15 '19 at 13:18
  • It really looks like you aren't allowed to set the ``\Deleted`` Flag. Did you try to set another flag? Maybe you should talk to the mailbox administrator. – Sascha May 15 '19 at 13:24
  • @Sascha the flag of \Seen is working(i.e read/uread). – Jennis Vaishnav May 15 '19 at 13:37
  • The server is restricting you for some reason. Check for some account or mailbox configuration that would control this, or talk to the server administrator. There's nothing wrong with your program. – Bill Shannon May 15 '19 at 16:06
  • @BillShannon good to know that there's nothing wrong with the code. I checked with xxx12@yahoo.co.jp settings, But didn't find anything useful. Nevertheless I'll again check in the settings page of yahoo.co.jp. – Jennis Vaishnav May 16 '19 at 03:58

2 Answers2

0

I think server is restricting you to some reason, so that's why you are not able to delete mail from mail server. You can contact your mail admin. Otherwise there is nothing wrong in java code.

Vimal
  • 411
  • 6
  • 28
0

I don't think code has any problem, it will be settings/permission related problem, Please check your mail configuration or permission.

Sam
  • 404
  • 1
  • 4
  • 18