jstedfast / MailKit

A cross-platform .NET library for IMAP, POP3, and SMTP.
http://www.mimekit.net
MIT License
6.04k stars 809 forks source link

MailKit Login Issue: Stalling on Inbox Access #1736

Closed Cegazhos closed 3 months ago

Cegazhos commented 3 months ago

I've noticed a strange issue with MailKit. There are two approaches. The first approach gets stuck for several minutes after successful login at this line 'client.Inbox.Open(FolderAccess.ReadWrite);'. The second approach logs in normally. Bad

private void YaDaZe(object sender, RoutedEventArgs e)
        {
            List<string> acc_list = new List<string>(File.ReadLines("./40.txt"));
            Do_Mails(acc_list);
        }

        private void Do_Mails(List<string> accounts)
        {
            List<string> tmp_ans;
            List<string> answer = new List<string>();
            string proxy_uri = "proxy_host";
            BinarySearchQuery query;
            IList<IMailFolder> folders;
            IList<MailKit.UniqueId> uids;
            DateSearchQuery basequery;
            string tmp_links;
            string form;
            List<string> succeed= new List<string> ();
            List<string> bad_net = new List<string>();
            string aaa = "proxy_account";
            string bbb = "proxy_password";
            foreach (var acc in accounts)
            {
                Debug.WriteLine($"Now is {acc}");
                    string[] parts = acc.Split(':');
                string email, password;
                email = parts[0];
                password = parts[1];
                using (var client = new ImapClient())
                {
                    var credential = new NetworkCredential(aaa, bbb);
                    var socks5_client = new Socks5Client(proxy_uri, 2333, credential);
                    client.ProxyClient = socks5_client;
                    client.CheckCertificateRevocation = false;
                    client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                    try
                    {
                        client.Connect("outlook.office365.com", 993, true);
                        client.Authenticate(email, password);
                    }
                    catch (MailKit.Security.AuthenticationException login_failed)
                    {
                        Debug.WriteLine("wrong~" + login_failed);
                        client.Disconnect(true);
                        continue;
                    }
                    catch (MailKit.Security.SslHandshakeException)
                    {
                        bad_net.Add(acc);
                        client.Disconnect(true);
                        continue;
                    }
                    catch
                    {
                        Debug.WriteLine($"{acc} something wrong");
                    }
                    client.Inbox.Open(FolderAccess.ReadWrite);
                    int ccc = client.Inbox.Count();
                    Debug.WriteLine(ccc);
                    Debug.WriteLine($"we got it{acc}");
                    succeed.Add(acc);
                    client.Disconnect(true);
                }
            }
            foreach (var sss in succeed)
                Debug.WriteLine(sss);
            Debug.WriteLine($"wrong is {bad_net.Count()}");
        }

Good

private void OneShot(object sender, RoutedEventArgs e)
        {
            string acc = "mail_acc:mail_pass"; // 20:57
            ImapClient client;
            Socks5Client socks5_client;
            string proxy_uri = "proxy_host";
            using (client = new ImapClient())
            {

                var credential = new NetworkCredential("proxy_acc", "proxy_pass");
                socks5_client = new Socks5Client(proxy_uri, 2333, credential);
                client.ProxyClient = socks5_client;
                string[] parts = acc.Split(':');
                client.CheckCertificateRevocation = false;
                client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                try
                {
                    client.Connect("outlook.office365.com", 993, true);
                    Debug.WriteLine($"+++{parts[0]}+++{parts[1]}---has connected");
                    client.Authenticate(parts[0], parts[1]);
                    Debug.WriteLine($"+++{parts[0]}+++{parts[1]}---got it");
                }
                catch
                {
                    Debug.WriteLine($"{acc} something wrong");
                    client.Disconnect(true);
                }
                client.Inbox.Open(FolderAccess.ReadOnly);
                int newEmailCount = client.Inbox.Count;
                Debug.WriteLine($"You have {newEmailCount}");
                Debug.WriteLine($"we got it{acc}");
                client.Disconnect(true);
            }
        }

        private void BigShot(object sender, RoutedEventArgs e)
        {
            string[] parts = "mail_acc:mail_pass".Split(":");
            string host = "outlook.office365.com"; // IMAP host for Outlook
            string email = parts[0];
            string password = parts[1];
            string aaa = "proxy_acc";
            string bbb = "proxy_pass";
            string proxy_uri = "proxy_host";

            using (var client = new ImapClient())
            {

                var credential = new NetworkCredential(aaa, bbb);
                var socks5_client = new Socks5Client(proxy_uri, 2333, credential);
                client.ProxyClient = socks5_client;
                client.CheckCertificateRevocation = false;
                client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
                try
                {
                    client.Connect("outlook.office365.com", 993, true);
                    Debug.WriteLine($"+++{email}+++{password}---has connected");
                    client.Authenticate(email,password);
                    Debug.WriteLine($"+++{email} +++ {password}---got it");
                }
                catch
                {
                    Debug.WriteLine($"something wrong");
                    client.Disconnect(true);
                }
                client.Inbox.Open(FolderAccess.ReadOnly);
                int newEmailCount = client.Inbox.Count;
                Debug.WriteLine($"You have {newEmailCount}");
                Debug.WriteLine($"we got it{email}");
                client.Disconnect(true);
            }
        }
Cegazhos commented 3 months ago

In the second approach, both functions operate normally. I still haven't figured out where the mistake lies. Even after consulting with ChatGPT, we haven't reached a conclusion.

jstedfast commented 3 months ago

Do you know how to use a debugger? If not, this would be the perfect time to learn.

You'll learn and grow so much more as a software developer if I let you figure this out on your own than if I tell you what mistake you made.

Once you figure it out, you'll be able to spot this type of bug without needing to run the code in a debugger, before you had your morning coffee or even gotten out of bed yet while looking at the code snippet on your phone's tiny screen and laying in bed.

I'd wish you luck, but I'm confident that you won't need luck because you'll have real skills 😇