Javamail problem with SEEN flags (Autmatically updates the server)
Hi all,
I am using Javamail 1.4.
When I read a message I used to get "java.io.IOException: No content" exceptions. Then I have created new MimeMessage from folder.getMessage() return as below
1) MimeMessage m = (MimeMessage)foldergetMessage(i);
2) MimeMessage mimeMsg = new MimeMessage(m);
After doing this I am seeing all my unread mails are shown as read in my server (thru' server's web access).
This is happening with step 2) only. If I comment out step 2, i will be getting others errors like, No content/BODY STRUCTRUE etc..
Am I doing anything wrong here?
# 1
I was getting javax.mail.MessagingException: Unable to load BODYSTRUCTURE error, so I followed the workaround given in the FAQ page http://java.sun.com/products/javamail/FAQ.html#imapserverbug
It says setFlags have no efftect on original message. I am not doing any setFlags in my code. But somehow the server gets updated of this SEEN flag. So once I execute my program all my unread messages in server are set as read irrespective of where I am setting the flag or not.
Pls help me!
# 2
When you copy the message, the new Message object has no connection withyour server. The flags are stored in the server, not in the message content.You need to use the original Message object to access the flags on your server.
# 3
Hi,
This is my client which reads the mail. I am using EmailArchitect as mail server.
I had few mails unread before executing this piece of code. But after when I saw the messages in server, they are shown as read.
package com.imap.client;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.search.*;
import javax.activation.*;
public class ReadMail {
public static void main(String[] args) throws IOException {
getMessages();
//getMessages();
}
private static void getMessages() throws IOException {
// SUBSTITUTE YOUR ISP's POP3 SERVER HERE!!!
String host = "mail.com";
// SUBSTITUTE YOUR USERNAME AND PASSWORD TO ACCESS E-MAIL HERE!!!
String user = "user@mail.com";
String password = "password";
// SUBSTITUTE YOUR SUBJECT SUBSTRING TO SEARCH HERE!!!
String subjectSubstringToSearch = "Test E-Mail through Java";
// Get a session. Use a blank Properties object.
Session session = Session.getInstance(new Properties());
try {
// Get a Store object
Store store = session.getStore("imap");
store.connect(host, user, password);
// Get "INBOX"
Folder fldr = store.getFolder("INBOX");
Folder defFldr = store.getDefaultFolder();
fldr = defFldr.getFolder("INBOX");
fldr.open(Folder.READ_ONLY);
int count = fldr.getMessageCount();
System.out.println(count + " total messages");
// Message numebers start at 1
for(int i = 1; i <= count; i++) {
// Get a message by its sequence number
//Message m = (MimeMessage)fldr.getMessage(i);
MimeMessage m = (MimeMessage)fldr.getMessage(i);
MimeMessage mimeMsg = new MimeMessage(m);
}
// "true" actually deletes flagged messages from folder
fldr.close(true);
store.close();
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
} //End of class
Thanks,
Rama
# 4
The reason the messages are getting marked as SEEN is because you've
seen them; you've fetched the entire message from the server.
BTW, I don't know what you're doing in this code:
Folder fldr = store.getFolder("INBOX");
Folder defFldr = store.getDefaultFolder();
fldr = defFldr.getFolder("INBOX");
You only need the first of those three lines.
# 5
You are right, those two lines of code are not requried.
I found that this piece of code makes the message as read automatically.
MimeMessage mimeMsg = new MimeMessage(m);
Is there any way out to avoid this problem? I don't want to make the message as read unless & until the message has been read from my client or actually read.
Thanks!
Rama
# 6
And also if you see the code, the folder is opened as READ_ONLY.
# 7
Sounds like you have a significantly broken IMAP server.
Try switching to a better IMAP server and see if that solves
your problem.
If a mailbox opened read-only is allowing permanent flags
to be changed, that's a serious bug. You can try resetting
the flag yourself, but who knows whether your buggy server
will allow you to do that.
# 8
Thanks for the info.
Now I moved to different mail server (Marek). Even now I am having some problems with Flags.
1) Open folder as READ_ONLY. This works fine. Flags are properly set.
2) Open folder as READ_WRITE. I have the same problem here. First time SEEN flag is not set in the messages that are not read yet/marked as unread. When I run my program again, I am seeing the SEEN flags are set for all messages (even if they are not read).
That too I am observing this problem with this piece of code.
MimeMessage mimeMsg = new MimeMessage(m);
If I comment this piece and run my program any number of times, I saw the flags are proper.
# 9
Mail server is "Merak" not marek.
# 10
I thought I explained this...
The line of code you're asking about is reading the entire message so
that it can make a copy. Thus, your mail server believes you read the
message.
Maybe your new mail server doesn't have the same bugs as your
old mail server and you can remove that line of code, which only existed
to work around the bug?