Discussion:
[Classpathx-javamail] Folder objects sharing same folder
foo
2007-08-22 22:18:43 UTC
Permalink
I am trying to append messages to an IMAP folder. My IMAP server, for some
reason, does not allow appending messages to a folder that is opened
read-only

If I do the following code:

Folder f = store.getFolder("foo");
f.appendMessages(msgs);

It succeeds, because you can append messages to a closed folder.

If I do the following code:

Folder f = store.getFolder("foo");
f.open(Folder.READ_ONLY);
f.appendMessages(msgs);

It fails, because you cannot append messages to a folder opened read-only.

But if I create another Folder object which points to the same folder
without opening it, it still fails:

Folder f = store.getFolder("foo");
f.open(Folder.READ_ONLY);
Folder g = store.getFolder("foo");
g.appendMessages(msgs);

Even though the Folder object "g" has not been "opened". Is this because
they both share the same connection or something? How do I work around this?

Thanks,
Chris Burdess
2007-08-30 21:48:01 UTC
Permalink
Post by foo
I am trying to append messages to an IMAP folder. My IMAP server, for some
reason, does not allow appending messages to a folder that is opened
read-only
Folder f = store.getFolder("foo");
f.appendMessages(msgs);
It succeeds, because you can append messages to a closed folder.
Folder f = store.getFolder("foo");
f.open(Folder.READ_ONLY);
f.appendMessages(msgs);
It fails, because you cannot append messages to a folder opened read-only.
But if I create another Folder object which points to the same folder
Folder f = store.getFolder("foo");
f.open(Folder.READ_ONLY);
Folder g = store.getFolder("foo");
g.appendMessages(msgs);
Even though the Folder object "g" has not been "opened". Is this because
they both share the same connection or something?
The IMAP server doesn't have a concept of folder object references. It
just refers to the folders by name. So once you have opened the folder
"foo" it becomes the current open folder, whether or not your code
refers to f or g. This will generally be true of most mail folder
implementations.
Post by foo
How do I work around this?
Generally the idea is that appendMessages works ith a folder that is not
the current folder you're working with. So you have references to
folders f and g by name (different names). Open f. Fetch some messages.
Append them to g. Close f.

This is the general algorithm you'd be using if you were developing a
mail user agent. I can see that there might be problems if you're just
using JavaMail in a more abstract sense. It might be worthwhile bringing
up your issues on the javamail-interest mailing list if this concerns
you.
--
Chris Burdess
Loading...