Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message is not marked as seen #153

Closed
Serproger opened this issue Feb 19, 2015 · 22 comments
Closed

Message is not marked as seen #153

Serproger opened this issue Feb 19, 2015 · 22 comments

Comments

@Serproger
Copy link

I'm trying to work with mail.ru via IMAP. I've wrote the code like

var info = messages.FirstOrDefault(item => item.SortableSubject == sentFiles[i]);
if (info != null)
{
    inbox.SetFlags(info.UniqueId.Value, MessageFlags.Seen, true);
    inbox.MoveTo(info.UniqueId.Value, sent);
}

to mark a message as seen and move it to the "Sent" folder, but after executing of this code the message remains unseen (through moving of the message works). Maybe is it a bug in MailKit? Please check it.

@jstedfast
Copy link
Owner

Add a ProtocolLogger to your ImapClient constructor to get a log of the IMAP protocol transaction to see what is going on.

using (var client = new ImapClient (new ProtocolLogger ("imap.log"))) {
    client.Connect (...);
    client.Authenticate (...);

    var inbox = client.Inbox;
    var sent = ...;

    inbox.Open (FolderAccess.ReadWrite);

    var messages = inbox.Fetch (...);

    // why are you using SortableSubject here? This is only meant to be used for sorting purposes
    // and is not necessarily the actual subject of the message. What you probably want is
    // info.Envelope.Subject instead.
    var info = messages.FirstOrDefault(item => item.SortableSubject == sentFiles[i]);
    if (info != null) {
        inbox.SetFlags(info.UniqueId.Value, MessageFlags.Seen, true);
        inbox.MoveTo(info.UniqueId.Value, sent);
    }
}

My bet is that MailKit is in fact marking the message as Seen assuming it is even matching the message you expect.

@Serproger
Copy link
Author

C: A00000008 SELECT INBOX
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * 1 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 1]
S: * OK [UIDVALIDITY 1370262345]
S: * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]
S: * OK [UIDNEXT 2460]
S: A00000008 OK [READ-WRITE] SELECT completed
C: A00000009 FETCH 1:2 (UID ENVELOPE)
S: * 1 FETCH (UID 2459 ENVELOPE ("Thu, 19 Feb 2015 16:28:10 +0300" "qqq2.txt" ((NIL NIL "__" "mail.ru")) NIL NIL ((NIL NIL "___" "mail.ru")) NIL NIL NIL "<__>"))
S: A00000009 OK FETCH done
C: A00000010 UID STORE 2459 FLAGS.SILENT ()
S: A00000010 OK STORE done
C: A00000011 UID MOVE 2459 __
S: * OK [COPYUID 1370262345 2459 2460]
S: * 1 EXPUNGE
S: * 0 EXISTS
S: A00000011 OK Done
C: A00000012 LOGOUT
S: * BYE logging out
S: A00000012 OK LOGOUT completed

I've replaced here some info that seems confidential with underscore signs.
P.S. Now I'm using info.Envelope.Subject

@jstedfast
Copy link
Owner

What is the value of inbox.PermanentFlags?

@Serproger
Copy link
Author

None

@jstedfast
Copy link
Owner

That's the problem, then... but no idea why it is None. This works perfectly fine in my unit tests.

@Serproger
Copy link
Author

Probably you can register a mailbox on mail.ru and test it on your own. I can't give you the full log or the full program :(

@jstedfast
Copy link
Owner

I've just pasted the following snippet into my Unit Tests and it works fine:

S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * 1 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 1]
S: * OK [UIDVALIDITY 1370262345]
S: * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]
S: * OK [UIDNEXT 2460]
S: A00000008 OK [READ-WRITE] SELECT completed

@jstedfast
Copy link
Owner

I can't read Russian so there's no way I'm going to figure out how to create an account on mail.ru's website.

@Serproger
Copy link
Author

Ok, i can register the test account and submit the credentials to you.

@jstedfast
Copy link
Owner

Thanks.

@Serproger
Copy link
Author

I've submitted the message to your mail address.

@jstedfast
Copy link
Owner

Awesome, thanks. I'll see if I can debug this using the mail.ru account...

@jstedfast
Copy link
Owner

This is very odd. I used the following test program and it worked as expected:

using System;
using System.Linq;

using MailKit.Net.Imap;
using MailKit;
using MimeKit;

namespace TestClient {
    class Program
    {
        public static void Main (string[] args)
        {
            var logger = new ProtocolLogger (Console.OpenStandardError ());

            using (var client = new ImapClient (logger)) {
                client.Connect ("imap.mail.ru", 993, true);

                Console.WriteLine ("Capabilities = {0}", client.Capabilities);

                client.AuthenticationMechanisms.Remove ("XOAUTH2");
                client.Authenticate ("xxx", "xxx");

                Console.WriteLine ("Capabilities = {0}", client.Capabilities);

                var inbox = client.Inbox;

                inbox.Open (FolderAccess.ReadWrite);

                Console.WriteLine ("PermanentFlags = {0}", inbox.PermanentFlags);
                Console.WriteLine ("AcceptedFlags = {0}", inbox.AcceptedFlags);

                var items = MessageSummaryItems.Full | MessageSummaryItems.UniqueId;
                var messages = inbox.Fetch (0, -1, items);
                var info = messages.FirstOrDefault (x => x.Envelope.Subject == "Test 1");

                if (info != null)
                    inbox.SetFlags (info.UniqueId.Value, MessageFlags.Seen, true);

                client.Disconnect (true);
            }
        }
    }
}

The log is as follows:

[fejj@localhost net40]$ mono ./mailru.exe 
Connected to imaps://imap.mail.ru:993/
S: * OK Welcome
C: A00000000 CAPABILITY
S: * CAPABILITY IMAP4rev1 ID XLIST UIDPLUS UNSELECT MOVE AUTH=PLAIN AUTH=XOAUTH2
S: A00000000 OK CAPABILITY completed
Capabilities = IMAP4rev1, Status, Id, Unselect, UidPlus, Move, XList
C: A00000001 AUTHENTICATE PLAIN
S: + 
C: xxx
S: * CAPABILITY IMAP4rev1 ID XLIST UIDPLUS UNSELECT MOVE
S: A00000001 OK Authentication successful
C: A00000002 LIST "" ""
S: * LIST (\NoSelect) "/" ""
S: A00000002 OK LIST done
C: A00000003 LIST "" "INBOX"
S: * LIST (\Inbox) "/" "INBOX"
S: A00000003 OK LIST done
C: A00000004 XLIST "" "*"
S: * XLIST (\Inbox) "/" "INBOX"
S: * XLIST (\Spam) "/" "&BCEEPwQwBDw-"
S: * XLIST (\Sent) "/" "&BB4EQgQ,BEAEMAQyBDsENQQ9BD0ESwQ1-"
S: * XLIST (\Drafts) "/" "&BCcENQRABD0EPgQyBDgEOgQ4-"
S: * XLIST (\Trash) "/" "&BBoEPgRABDcEOAQ9BDA-"
S: A00000004 OK XLIST done
Capabilities = IMAP4rev1, Status, Id, Unselect, UidPlus, Move, XList
C: A00000005 SELECT INBOX
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * 1 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 1]
S: * OK [UIDVALIDITY 1424354903]
S: * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft)]
S: * OK [UIDNEXT 8]
S: A00000005 OK [READ-WRITE] SELECT completed
PermanentFlags = Seen, Answered, Flagged, Deleted, Draft
AcceptedFlags = Seen, Answered, Flagged, Deleted, Draft
C: A00000006 FETCH 1:* (UID FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY)
S: * 1 FETCH (UID 7 FLAGS (\Unseen) INTERNALDATE "19-Feb-2015 17:22:45 +0300" RFC822.SIZE 3025 ENVELOPE ("Thu, 19 Feb 2015 09:22:40 -0500" "Test 1" (("Jeff Stedfast" NIL "xxx" "xxx")) NIL NIL ((NIL NIL "xxx" "xxx")) NIL NIL NIL "<CALqoN0YdNfvykd8ngqug4cjhZ-pEUnaYxSkXJbLWx9CNQxvY+g@mail.gmail.com>") BODY (("text" "plain" ("charset" "utf-8") NIL NIL "7-bit" 24 0)("text" "html" ("charset" "utf-8") NIL NIL "7-bit" 60 0) "alternative"))
S: A00000006 OK FETCH done
C: A00000007 UID STORE 7 FLAGS.SILENT (\Seen)
S: A00000007 OK STORE done
C: A00000008 LOGOUT
S: * BYE logging out
S: A00000008 OK LOGOUT completed

I wonder if the bug you are seeing has something to do with your locale charset. I assume that it's iso-8859-5, koi8-r or windows-cp1251 or something other than UTF-8, correct?

@Serproger
Copy link
Author

This program also works fine on my computer. This is very strange...
Yes, default encoding is koi8-r.

@jstedfast
Copy link
Owner

Hmmm, not sure that would matter... ImapStream's ReadToken() logic is all ascii-based anyway (at least for flags), it just casts bytes to chars, so there's no way that this should be a charset conversion issue there unless switch statements on strings are problematic?

The code to map flag strings to enum bits looks like this:

string flag = (string) token.Value;
switch (flag) {
case "\\Answered": flags |= MessageFlags.Answered; break;
case "\\Deleted": flags |= MessageFlags.Deleted; break;
case "\\Draft": flags |= MessageFlags.Draft; break;
case "\\Flagged": flags |= MessageFlags.Flagged; break;
case "\\Seen": flags |= MessageFlags.Seen; break;
case "\\Recent": flags |= MessageFlags.Recent; break;
}

The only thing I can think of is that the CurrentCulture has some effect on the switch statement above that is making it not work as expected. I would expect, though, that it wouldn'tmatter... at least for koi8-r since koi8-r still contains the ASCII subset which is all that is being compared ehre and afaik has no Hungarian-i-like oddities.

@Serproger
Copy link
Author

I've found the problem. Before inbox folder processing I've opened 'Sent' folder and haven't closed it. I've added sent.Close() and now everything works fine. But I think a exception should be raised if such a situation leads to errors.

@jstedfast
Copy link
Owner

Can you give me some pseudo code? I'm confused about the sent folder thing.

@Serproger
Copy link
Author

using System;
using System.Linq;
using MailKit.Net.Imap;
using MailKit;
using MimeKit;
using System.Text;

namespace TestClient
{
    class Program
    {
        public static void Main(string[] args)
        {
            var logger = new ProtocolLogger(Console.OpenStandardError());
            Console.WriteLine(Encoding.Default.BodyName);
            using (var client = new ImapClient(logger))
            {
                client.Connect("imap.mail.ru", 993, true);

                Console.WriteLine("Capabilities = {0}", client.Capabilities);

                client.Authenticate("xxx", "xxx");
                Console.WriteLine("Capabilities = {0}", client.Capabilities);

                var sent = client.GetFolder(SpecialFolder.Sent);
                sent.Open(FolderAccess.ReadWrite);

                var inbox = client.Inbox;

                inbox.Open(FolderAccess.ReadWrite);

                Console.WriteLine("PermanentFlags = {0}", inbox.PermanentFlags);
                Console.WriteLine("AcceptedFlags = {0}", inbox.AcceptedFlags);

                var items = MessageSummaryItems.Envelope | MessageSummaryItems.UniqueId;
                var messages = inbox.Fetch(0, -1, items);
                var info = messages.FirstOrDefault(x => x.Envelope.Subject == "Test 1");

                if (info != null)
                    inbox.SetFlags(info.UniqueId.Value, MessageFlags.Seen, true);

                client.Disconnect(true);
            }
        }
    }
}

This code gives the described problem.

@jstedfast
Copy link
Owner

Okay, thanks.

jstedfast added a commit that referenced this issue Feb 19, 2015
…er folder was selected

Instead, set the previously selected folder's PermFlags to None. D'oh!

Fixes issue #153
@jstedfast
Copy link
Owner

Thanks, that revealed the bug in ImapFolder.Open() clear as day. I've fixed my think-o in the code now, so all should be good.

@Serproger
Copy link
Author

Can you publish the new version in the Nuget?

@jstedfast
Copy link
Owner

Sure. Just published 1.0.8 to NuGet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants