Marking messages as read

  21 posts   Feedicon  
Replies: 20 - Last Post: March 20, 2011 05:59
by: ashishdhawan
showing 1 - 20 of 21
« Previous 1
 
Posted: November 19, 2010 06:26 by Rajiv
The Flags.Flag.SEEN is used to get unread messages but what is the Flags.Flag that should be set to a message making it as read, is it Flags.Flag.ANSWERED or Flags.Flag.FLAG ?
 
Posted: November 19, 2010 22:41 by shannon
SEEN == read

The SEEN flag indicates whether or not the message has been read.
 
Posted: November 20, 2010 11:12 by Rajiv
thanks for the reply shannon. but lets assume the following scenario and explain me how the SEEN flag indicates whether or not the message has been read?

Folder folder = store.getFolder();
folder.open(Folder.READ_ONLY);
for (final Message msg : folder.search(new FlagTerm(
new Flags(Flags.Flag.SEEN), false))) {
Message m = msg; // --> is this a unread message ?
// if so what is the flag we set to make it as read. is it like the following
msg.setFlag(Flags.Flag.ANSWERED, true);
}

 
Posted: November 21, 2010 01:47 by shannon
I don't understand why you're confused. You set the SEEN flag to mark it as read:

msg.setFlag(Flags.Flag.SEEN, true);

Note that if you've opened the folder read-only, as in your example, your flag changes will not be permanent,
if they're allowed at all.

And in case you're really confused, let's be clear... Just because the SEEN flag is set doesn't mean any
human has actually seen the message, let alone read the content of the message.
 
Posted: November 21, 2010 03:34 by Rajiv
thanks for the reply. But what i wanted to know was that how do u know if a message is read or not in imap? As you pointed out setting the SEEN flag to mark it as read answeres one question but how do you know if that message is still in unread status.Do we check like if(!message.isSet(Flags.Flag.SEEN)){} ?
 
Posted: November 21, 2010 04:35 by shannon
I guess I'm still not understanding what you're asking...

If the SEEN flag is set, that means the message is read.

If the SEEN flag is not set, that means the message is not read, that is, is unread.

Is there some reason this is confusing?

So, yes, if you want to know if a message is unread, check if (!msg.isSet(Flags.Flag.SEEN)).
 
Posted: November 21, 2010 08:23 by Rajiv
thanks shannon the last line of your answer is what i was looking for.By the way just a thought why doesn't the javamail API provide a separate flag for unread messages such as lets say "UNSEEN". Is their a reason for that?
 
Posted: November 21, 2010 21:00 by shannon
You want both a SEEN flag and an UNSEEN flag? So what does it mean if they're both set?

You do understand that you can check both for a flag being set and for a flag not being set,
and if the condition you're looking for is the opposite of what a flag indicates, you should check
for the flag not being set, right?

For example, if you're looking for all the message that you haven't replied to (answered), you
should look for messages where the ANSWERED flag is not set (false).
 
Posted: November 22, 2010 06:45 by Rajiv
I totally agree with your point but my argument was only about the read/unread flag. Sorry to bug you this much since your the only person in the net to answer javamail related questions so i have no option but to post it this forum.

>>So what does it mean if they're both set?
it should not allow to do that at all

Also can a message be set with multiple flags?



 
Posted: November 23, 2010 06:21 by shannon
>>So what does it mean if they're both set?
> it should not allow to do that at all

And what if neither is set?

Is it really that difficult to understand that one is the opposite of the other?

> Also can a message be set with multiple flags?

Yes.

BTW, have you found the JavaMail Forum?
http://forums.oracle.com/forums/forum.jspa?forumID=975
 
Posted: November 24, 2010 14:24 by Rajiv
what i am asking is something like the following

// if the passed message is set with seen (it is read message)
if (message.isSet(Flags.Flag.SEEN)) {
// now make that message as unread
message.setFlag(<??? which flag to set ? >, true);
}

java forum is useful but the answers are not that good.
 
Posted: November 24, 2010 15:25 by Rajiv
i found out and understood it for unread mail i have to use message.setFlag(Flags.Flag.SEEN, false);
 
Posted: March 03, 2011 08:28 by ashishdhawan
Hi Bill,

My requirement is to retrieve only new/unread messages from a POP3 server. Following are the approaches I found in the javadocs for doing this:

1. A simple approach would be to keep track of the newest message seen by the application.

Could you please explain in detail on how this can be achieved?

One way of doing this could be store the recieved date but I found the following statement in the javadocs which states that POP3 does not support recieved date. Could we use some other header fields to achieve this (sent date,... )?

"POP3 does not provide a "received date", so the getReceivedDate method will return null. It may be possible to examine other message headers (e.g., the "Received" headers) to estimate the received date, but these techniques are error-prone at best"


2. An alternative would be to keep track of the UIDs (see below) of all messages that have been seen.

I'm currently using a similar approach, but the issue with this approach is all messages are retrieved from the POP3 server every time a retrieve operation is done. Is there a way to fetch only selected messages based on their UUID/message-id/any other unique id?

3. Another approach is to download all messages into a local mailbox, so that all messages in the POP3 mailbox are, by definition, new.

Is it possible to download messages to a local mailbox using JavaMail?

Thanks,
Ashish.
 
Posted: March 04, 2011 06:49 by shannon
1. The date doesn't matter. Newer messages will always be added to the folder after
the last message. You just need to keep track of enough identifying information for the
last message you saw that you can find it again when you examine the folder later.

2. You can retrieve the UIDs of messages without retrieving the messages.

3. There are many different approaches to managing a "local" mailbox. One would be
to simply run an IMAP mail server on the local machine and store messages there.
Another would be to use a "local store provider" for JavaMail that simply stores messages
in a file without needing a server. The JavaMail Third Party Products page lists
several local store providers. The JavaMail source repository also includes an mbox local store
provider, but you'll need to build it yourself. Finally, you could store the individual messages
in separate files, or in a local database.

Choosing between these options depends on the other requirements of your application.
 
Posted: March 16, 2011 15:03 by ashishdhawan
Thanks for the quick response! I have a few doubts regarding the first two points:

1. Even if the last message seen is identified, is it possible to retrieve ONLY the messages or message headers of the messages which were received after the last message seen? If yes, then could you please explain how this can be done?

2. Could you please explain how this could be done? Is this possible to retrieve other headers of the message without retrieving the message?

-Ashish
 
Posted: March 16, 2011 21:15 by shannon
1. Figure out the message number for the last message you've seen, then ask for messages
with message numbers larger than that.

2. With POP3 your choices are - get all the headers, or get the entire message.
Normally, the first time you ask for any header in the message, it will get all the
headers for the message. If you never access the body of the message, it will
never fetch the body of the message.
 
Posted: March 17, 2011 17:23 by ashishdhawan
1. The message number of the last seen message cannot be used to identify the last seen message as this number can change when messages are deleted from the folder, so one way I can think of retrieving the last seen message is by searching on another field which uniquely identifies the last seen message.

There is one issue with this approach though:

The javadoc for the Folder.search() API states that:

"This implementation invokes search(term, getMessages()), to apply the search over all the messages in this folder"

This does not seem very efficient as all the messages would be retrieved for the search.

Is there a better way to do the search or to identify the message number of the last seen message?

-Ashish
 
Posted: March 18, 2011 05:27 by shannon
It depends on whether your application is the only application accessing the folder.
If it is not, you're right, the message number is not enough. But you can get the UIDs
for all messages in the folder and compare them with the remembered UID for the last
message you've seen.

You don't want to use search.

Use the fetch method with the UID item, then get the Message objects for the POP3
folder, cast the folder to POP3Folder, and use the getUID method on each Message.
 
Posted: March 19, 2011 20:15 by ashishdhawan
Thanks Bill!

I have a few more queries:

1. My understanding of the fetch method is that it retrieves the UID along with the message from the server when getMessages() is called and not when message.getUID is called. Is my understanding correct?

2. On using FetchProfile.Item.ENVELOPE in the fetch method, would all the header fields be retrieved from the server along with the messages on calling getMessages()?

3. Would message numbers be prefetched on using FetchProfile.Item.ENVELOPE?

4. Which API(s) should be used to retrieve messages whose message numbers are greater than a particular number?

5. What is the default ordering of the messages when they are retrieved using getMessages()? Can they be ordered on the message number?

-Ashish
 
Posted: March 19, 2011 21:18 by shannon
1. What happens with fetch depends on the FetchProfile you use.
If you only ask for the UID, it only fetches the UIDs for the messages.
They're saved so that you can access them later via POP3Folder.getUID(Message).
The Folder.getMessages() method returns Message objects without accessing
any of the message data from the server. The message data is accessed on
demand unless it was prefetched with the fetch method.

2. Asking for the ENVELOPE causes all the headers to be fetched.

3. There's no reason to "fetch" message numbers, you already know them.
Once you know how many messages are in the folder (N), the message
numbers are 1..N.

4. Use the Folder.getMessages method that takes a range of message numbers.

5. The ordering is the ordering the messages appear in the mailbox on the server.
Almost always that it the order they arrived at the mail server, which is not necessarily
the order in which they were sent. If you want any other order, you have to sort them
yourself.
showing 1 - 20 of 21
« Previous 1
Replies: 20 - Last Post: March 20, 2011 05:59
by: ashishdhawan
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120518.3c65429)
 
 
Close
loading
Please Confirm
Close