jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.

Home Page:http://www.mimekit.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ImapClient CountChange USE

aaa19940608 opened this issue · comments

  • .NET Framework: .Net Framework 4.0
  • MailKit Version: 1.22.0.0

Hello!
1、I use the ImapClient.Inbox.CountChange event, and after binding it, sometimes it triggers the CountChange event when I receive emails in my inbox, but most of the time, it doesn't (most of the time it doesn't trigger),"MessagesArrived" is the same, sometimes it triggers, but most of the time it doesn't trigger.

2、When I use client.GetFolder(SpecialFolder.Sent), it can find the folder, but after binding the CountChange, it has never successfully triggered.

3、How can I resolve the above issues? Due to some reasons, I cannot upgrade the .Net Framework version and Mailkit version in my environment.

My c# code:

 public Form1()
        {
            InitializeComponent();
            var cts = new CancellationTokenSource();            
            Watch(cts.Token);        
        }

public static void Watch(CancellationToken cancellationToken)
        {
            using (var client = ClientCreateAndConnect(cancellationToken))
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    using (var done = new CancellationTokenSource())
                    {
                        using (var timer = new System.Timers.Timer(9 * 60 * 1000))
                        {
                            timer.Elapsed += (sender, e) => done.Cancel();
                            timer.AutoReset = false;
                            timer.Enabled = true;
                            try
                            {
                                client.Idle(done.Token, cancellationToken);                 
                            }
                            catch (OperationCanceledException)
                            {
                                break;
                            }
                        }
                    }
                }

            }
        }


private static ImapClient ClientCreateAndConnect(CancellationToken token)
        {
            var client = new ImapClient();
            client.Connect("xxx.xx.com", 993, true, token);
            client.ServerCertificateValidationCallback = (s, c, h, e) => true;
            client.Authenticate("xx@xx.com", "xx", token); 

            var folder = client.Inbox;
            folder.Open(MailKit.FolderAccess.ReadOnly, token);
            folder.CountChange+= client_CountChanged;

            var folder2 = client.GetFolder(SpecialFolder.Sent);
            folder2.Open(MailKit.FolderAccess.ReadWrite, token);
            folder2.CountChange+= client_CountChanged;
           
            return client;
        }

public static void client_CountChanged(object sender, EventArgs e)
        {
            xxxx;
        }

Dear jstedfast , is there anything wrong with my code? Is there a way to resolve it? Thank you very much!

After logging in, I entered IDLE mode, then sent an email and deleted emails from the outbox, but none of these actions triggered the corresponding events. Below is the log after the operations were completed, but there has been no further log output since entering IDLE mode.

Connected to imaps://xxx.xx.com:993/
S: * OK [CAPABILITY IMAP4 IMAP4rev1 ID AUTH=PLAIN AUTH=LOGIN AUTH=XOAUTH2 NAMESPACE] xx XMIMAP4Server ready
C: A00000000 AUTHENTICATE PLAIN
S: + 
C: ADU5Mjk2MjU4NkBxcS5jb20AdmFkeXV3YXBmc21pYmJjaQ==
S: A00000000 OK Success login ok
C: A00000001 CAPABILITY
S: * CAPABILITY IMAP4 IMAP4rev1 XLIST MOVE IDLE XAPPLEPUSHSERVICE NAMESPACE CHILDREN ID UIDPLUS COMPRESS=DEFLATE
S: A00000001 OK CAPABILITY Completed
C: A00000002 NAMESPACE
S: * NAMESPACE (("" "/")) NIL NIL
S: A00000002 OK NAMESPACE Success
C: A00000003 LIST "" "INBOX"
S: * LIST (\HasNoChildren) "/" "INBOX"
S: A00000003 OK LIST completed
C: A00000004 XLIST "" "*"
S: * XLIST (\NoSelect \HasChildren) "/" "&xx-"
S: * XLIST (\HasNoChildren \Inbox) "/" "INBOX"
S: * XLIST (\HasNoChildren \Sent) "/" "Sent Messages"
S: * XLIST (\HasNoChildren \Drafts) "/" "Drafts"
S: * XLIST (\HasNoChildren \Trash) "/" "Deleted Messages"
S: * XLIST (\HasNoChildren \Junk) "/" "Junk"
S: A00000004 OK XLIST completed
C: A00000005 LIST "" "*"
S: * LIST (\NoSelect \HasChildren) "/" "&UXZO1mWHTvZZOQ-"
S: * LIST (\HasNoChildren) "/" "INBOX"
S: * LIST (\HasNoChildren) "/" "Sent Messages"
S: * LIST (\HasNoChildren) "/" "Drafts"
S: * LIST (\HasNoChildren) "/" "Deleted Messages"
S: * LIST (\HasNoChildren) "/" "Junk"
S: A00000005 OK LIST completed
C: A00000006 SELECT &UXZO1mWHTvZZOQ-
S: * 0 EXISTS
S: * 0 RECENT
S: A00000006 OK [READ-ONLY] SELECT complete
C: A00000007 SELECT INBOX
S: * 2810 EXISTS
S: * 0 RECENT
S: * OK [UNSEEN 377]
S: * OK [UIDVALIDITY 1433844951] UID validity status
S: * OK [UIDNEXT 4510] Predicted next UID
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * OK [PERMANENTFLAGS (\* \Answered \Flagged \Deleted \Draft \Seen)] Permanent flags
S: A00000007 OK [READ-WRITE] SELECT complete
C: A00000008 SELECT "Sent Messages"
S: * 324 EXISTS
S: * 0 RECENT
S: * OK [UIDVALIDITY 1433844951] UID validity status
S: * OK [UIDNEXT 423] Predicted next UID
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * OK [PERMANENTFLAGS (\* \Answered \Flagged \Deleted \Draft \Seen)] Permanent flags
S: A00000008 OK [READ-WRITE] SELECT complete
C: A00000009 IDLE
S: + idling

MailKit depends on the server sending an # EXISTS event which MailKit then translates into a C# CountChanged event on the folder.

Are you suggesting that it's an issue with the email server being unstable rather than a problem with my code? Is that correct?

Thank You!