Setting flags?

Jul 8, 2011 at 10:44 AM

Hi there,

Not sure if this is an issue, or it's me. I'm setting the correct flag (As far as i'm aware) but it doesn't seem to make a blind bit of difference. The message still comes back as unseen.

Great lib though, perfect. If only this worked :-)

Cheers,

D

	Crystalbyte.Equinox.Imap.ImapClient client = new Crystalbyte.Equinox.Imap.ImapClient();
	
	//Connect
	client.Connect("mail.XXXXX.com", 143);
	
	//Authenticate
	bool authed = client.Authenticate("XXXX", "XXXX", Crystalbyte.Equinox.Security.Authentication.SaslMechanics.Login);
	
	if (!authed)
		throw new Exception("Failed to authenticate");
	
	//Select the mailbox
	if (client.Select("INBOX").IsOk)
	{	
		Crystalbyte.Equinox.Imap.Responses.SelectExamineImapResponse res = client.Examine(client.SelectedMailbox);
		Console.Write(res.MailboxInfo.Exists + " Emails exist");

		SequenceSet set = client.Search("(UNSEEN)").SequenceSet;
		foreach (int sequenceNumber in set.Values.Take(10))
		{
			try
			{
				//Fetch the mail
				var msg = client.FetchMessageBySequenceNumber(sequenceNumber);
				
				//Update the store status
				client.Store(SequenceSet.CreateSet(sequenceNumber), MessageFlags.Seen, StoreProcedures.Add);
			}
			catch(Exception ex)
			{
				Console.WriteLine("Id: " + sequenceNumber.ToString() + " - " + ex.Message);
				
				//Failed to get this message, for some reason, let's fetch it
				var fetch = client.Fetch(SequenceSet.CreateSet(sequenceNumber), "");
				Console.Write(fetch.Text);
			}
		}
		
	}
	else
		throw new Exception("Failed to Selected 'INBOX'");

Coordinator
Jul 8, 2011 at 11:42 AM

Hey there,

japanfred wrote:

Great lib though, perfect. If only this worked :-)

Thanks very much and I'm sure we can figure this one out :)

I double checked the store command and it works fine on my end.

I recommend using the Debug assemblies if you are not already using them, since they log every in and output to and from the server in the Debug View.

It will be helpful if you could post your log, it should look something like this:

13:30:14 << * OK IMAP server ready H mimap6 87367
13:30:14 =>> a00 CAPABILITY
13:30:14 << * CAPABILITY IMAP4rev1 CHILDREN ENABLE ID IDLE LITERAL+ NAMESPACE QUOTA SORT THREAD=ORDEREDSUBJECT UIDPLUS UNSELECT WITHIN STARTTLS AUTH=LOGIN AUTH=PLAIN
13:30:14 << a00 OK CAPABILITY completed
13:30:14 =>> a01 STARTTLS
13:30:15 << a01 OK Begin TLS negotiation now
13:30:15 =>> a02 CAPABILITY
13:30:15 << * CAPABILITY IMAP4rev1 CHILDREN ENABLE ID IDLE LITERAL+ NAMESPACE QUOTA SORT THREAD=ORDEREDSUBJECT UIDPLUS UNSELECT WITHIN AUTH=LOGIN AUTH=PLAIN
13:30:15 << a02 OK CAPABILITY completed
13:30:15 =>> a03 LOGIN XXXXXXXXXX XXXXXXXXX
13:30:15 << a03 OK LOGIN completed
13:30:15 =>> a04 SELECT "INBOX"
13:30:15 << * 117 EXISTS
13:30:15 << * 0 RECENT
13:30:15 << * FLAGS (\Answered \Flagged \Deleted \Seen \Draft Junk NonJunk)
13:30:15 << * OK [PERMANENTFLAGS (\Answered \Flagged \Draft \Deleted \Seen Junk NonJunk \*)] Unlimited
13:30:15 << * OK [UNSEEN 23] Message 23 is first unseen
13:30:15 << * OK [UIDNEXT 247] Predicted next UID
13:30:15 << * OK [UIDVALIDITY 1298465306] UIDs valid
13:30:15 << a04 OK [READ-WRITE] SELECT completed
13:30:15 =>> a05 STORE 23 +FLAGS (\Seen)
13:30:15 << * 23 FETCH (FLAGS (\Flagged \Seen Junk))
13:30:15 << a05 OK STORE completed
The program '[6572] Crystalbyte.Equinox.Sandbox.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[6572] Crystalbyte.Equinox.Sandbox.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

Regards,
Alexander

Jul 8, 2011 at 12:04 PM

Interesting, didn't know the debug ones did that, handy!

13:00:46 << * OK Microsoft Exchange Server 2003 IMAP4rev1 server version 6.5.7638.1 (XXX.XXX.com) ready.
13:00:46 =>> a00 CAPABILITY
13:00:46 << * CAPABILITY IMAP4 IMAP4rev1 IDLE LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE LITERAL+ UIDPLUS CHILDREN AUTH=NTLM
13:00:46 << a00 OK CAPABILITY completed.
13:00:46 =>> a01 LOGIN XXXX XXXX
13:00:46 << a01 OK LOGIN completed.
13:00:46 =>> a02 SELECT "INBOX"
13:00:46 << * 7882 EXISTS
13:00:46 << * 1 RECENT
13:00:46 << * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
13:00:46 << * OK [PERMANENTFLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)] Permanent flags
13:00:46 << * OK [UNSEEN 1] Is the first unseen message
13:00:46 << * OK [UIDVALIDITY 20551] UIDVALIDITY value
13:00:46 << a02 OK [READ-WRITE] SELECT completed.
13:00:46 =>> a03 EXAMINE INBOX
13:00:46 << * 7882 EXISTS
13:00:46 << * 0 RECENT
13:00:46 << * FLAGS (\Seen \Answered \Flagged \Deleted \Draft $MDNSent)
13:00:46 << * OK [PERMANENTFLAGS ()] Permanent flags
13:00:46 << * OK [UNSEEN 1] Is the first unseen message
13:00:46 << * OK [UIDVALIDITY 20551] UIDVALIDITY value
13:00:46 << a03 OK [READ-ONLY] EXAMINE completed.
13:00:46 =>> a04 SEARCH (UNSEEN)
13:00:46 << * SEARCH 1 2 3 4 5
13:00:46 << a04 OK SEARCH completed.
--FETCHES MESSAGE HERE--
13:00:47 =>> a06 STORE 1 +FLAGS (\Seen)
13:00:47 << * 1 FETCH (FLAGS (\Seen))
13:00:47 << a06 OK STORE completed.

Looking at that, it seems to be storing the flag. However when i re-run the app, it still returns as part of my Search for Unseen messages!

Coordinator
Jul 8, 2011 at 1:22 PM

Well,

the Exchange Server is known to behave not always as expected, try searching for "NOT SEEN" instead of "UNSEEN".

Alex

Jul 11, 2011 at 11:03 AM

How bizzare. It's not the search this isn't working, it's the set of the flags.

Using another IMAP library (ImapX) it works as expected, going to debug Equinox and see what's happening... very weird!

Jul 11, 2011 at 2:19 PM

:) It was just my mis-understanding of the IMAP protocol. After reading the RFC, all is sorted.

As stated, an EXAMINE is the same as a SELECT command. However, EXAMINE sets the current connection to ReadOnly. That's my problem, sorry to have caused you pain. Maybe worth throwing an exeption if the user is in read-only mode, so it doesn't happen to others?

Regards,

Dean

Coordinator
Jul 11, 2011 at 9:39 PM
Edited Jul 11, 2011 at 10:27 PM

Hi Dean,

sry, I missed that one too, but I'm glad you could sort things out.

I thank you for your suggestion, but it has a huge drawback, which in my oppinion, outweights the benefits.

I try to keep the client stateless for the very reason, that the user is able to change server states without using the integrated methods like select or examine, I will even remove the SelectedMailbox property in the next release.

The user could for instance, if the server supports it, do an "Unselect" by sending "UNSELECT" using the regular "SendAndReceive()" method to the server which would put client and server "out of sync".

The client would reflect a selected mailbox, while the server already cleared the selection, which I believe would be even more confusing, therefor the client will not keep track of the users activities.

It is however an issue of the Exchange server, since it should return "NO" as a response and not "OK".

Regards

Alex

Jul 13, 2011 at 7:59 AM

Hey,
Ah yes, you're correct :-) Good old Microsoft, implementing the specification how they see fit.
Thanks again for a Great library, the IMAP Idle works really well too.
Cheers,
D


From: krasshirsch [email removed]
Sent: Monday, July 11, 2011 10:40 PM
To: Dean Thomas
Subject: Re: Setting flags? [equinox:264396]

From: krasshirsch

Hi Dean,

sry, I missed that one too, but I'm glad you could sort things out.

I thank you for your suggestion, but it has a huge drawback, which in my oppinion, outweights the benefits.

I try to keep the client stateless for the very reason, that the user is able to change server states without using the integrated methods like select or examine, I will even remove the SelectedMailbox property in the next release.

The user could for instance, if the server supports it, do an "Unselect" by sending "UNSELECT" using the regular "SendAndReceive()" method to the server that would put client and server "out of sync".

The client would reflect a selected mailbox, while the server already cleared the selection, which I believe would be even more confusing.

It is however an issue of the Exchange server, since it should return "NO" as a response and not "OK".

Regards

Alex