This project is read-only.

CRASH BUG, Verify if client logged in successfully?

Aug 9, 2010 at 10:26 AM
Edited Aug 9, 2010 at 3:30 PM
Here is my code. 
I am coding in C#, using framework 3.5, and InterIMAP-Async.
I am using SSL, inserting the cert before connecting/removing it after connection, and am in the process of optimizing my code.

Things I need to know:
1) How to verify if a client successfully logged in..? I am using a hackish method with timers...
2) Is there is a better way to determine if a connection is established?
3) I also can't figure out how to display server return messages (login failed, bad user pass, ssl required, connection failed, etc).
4) All of the variables I tried in MessageBox.Show() seem useless to client login but seem useful for host connection? When I am not logged in, but the server is up, everything is still set as if everything is ready to go and perfect.
5) URGENT, once I receive a new email, before I check it, my application crashes due to the InterIMAP.dll.
Here is a generic Windows Debug:
Problem Event Name: CLR20r3  
Problem Signature 01: check_email.exe  
Problem Signature 04: mscorlib  
Problem Signature 09: System.ArgumentException  

When I Debug with Visual Studio 2010, I get System.ArgumentException:
Requested value 'Recent' was not found.
Source: mscorlib
StackTrace:    at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)   at InterIMAP.Common.Processors.MessageFlagProcessor.ProcessFlags()   at InterIMAP.Common.Processors.MessageFlagProcessor.ProcessResult()   at InterIMAP.Common.Requests.BaseRequest.RunProcessor()   at InterIMAP.Asynchronous.Client.IMAPConnectionWorker.DoRequest(IRequest req)   at InterIMAP.Asynchronous.Client.IMAPConnectionWorker.DoWork()   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)   at System.Threading.ThreadHelper.ThreadStart()
TargetSite: {System.Object Parse(System.Type, System.String, Boolean)}

Honestly, not sure if it is my code, but it very well could be. If my program detects there is no e-mail, then I send one to my box, once it detects a new mail, it throws that exception.
If I send the e-mail to my account, THEN start the program from fresh, it still crashes.
If I view the e-mail in my web browser, then set it to "Unread" in the web browser, it then has no problems detecting the e-mails...WEIRD.


Thanks.

        private void start()
        {
            g_UnSeenMSGS = 0;
            g_Total = 0;
            g_Cur = 0;
            g_Found = false;

            SetVoidCallback aa = new SetVoidCallback(SetProgressBarMarquee); this.Invoke(aa);
            SetBoolCallback bb = new SetBoolCallback(SetProgressBarVisibility); this.Invoke(bb, new object[] { true });
            SetBoolCallback cc = new SetBoolCallback(SetSSLVisibility);
            SetBoolCallback dd = new SetBoolCallback(SetALVisibility);
            SetBoolCallback ff = new SetBoolCallback(SetVWVisibility); this.Invoke(ff, new object[] { false });
            SetTextCallback d = new SetTextCallback(SetText);
            SetBoolCallback ee = new SetBoolCallback(SetProgressBarVisibility);
            SetBoolCallback g = new SetBoolCallback(SetTimer3Enabled);
            SetBoolCallback i = new SetBoolCallback(SetCheckEnabled);
            SetTextCallback h = new SetTextCallback(SetName);

            //string FileName = g_Drive + ".autorun\\_data\\3_mail\\EMail.imap";
            string FileName = "EMail.imap";
            if (!File.Exists(FileName))
            {
                DialogResult result = BetterDialog.ShowDialog("Check Email", "An error occurred.", "\n" + "Email.imap file is missing!" + "\n\nIf you have any problems, please contact Caesar@MilesMilitusCallidus.com.\nThank you.", "", "Close", Properties.Resources.GMail1);
                this.notifyIcon1.Visible = false;
                Environment.Exit(0);
            }

            /* Certificate: INSTALL */
            Custom.Execute_Silent_App("Certificate_Install.bat");

            /* CONFIGURATION: loaded here */
            IMAPConfig config = new IMAPConfig(FileName);
            config.Host = Encryption.Decrypt(config.Host);
            config.UserName = Encryption.Decrypt(config.UserName);
            config.Password = Encryption.Decrypt(config.Password);
            if (config.UseSSL) this.Invoke(cc, new object[] { true });

            //Set Username globally
            string[] res = config.UserName.Split(new Char[] { '@' }); g_Name = res[0]; this.Invoke(h, new object[] { g_Name });

            //MessageBox.Show("HostName: " + config.Host + "\nUsername: " + config.UserName + "\nPassword: " + config.Password + "\nSSL?: " + config.UseSSL);

            /* CONNECT: Connect Here */
            client = new IMAPAsyncClient(config, 1); // 5 represents the number of worker connections to create. Depending on your bandwidth this number might need to be adjusted for optimal performance
            client.Start();
            
            //MessageBox.Show("Workers: " + client.NumberOfWorkers + "\n" + "ReadyToGo: " + client.ReadyToGo + "\n" + "IsAlive: " + client.IsAlive + "\n" + "WeHaveAProblem: " + client.WeHaveAProblem + "\n" + "Logs: " + client.Aggregator.Logs.ToString() + "\n" + "Debug Detail: " + client.Config.DebugDetail + "\n" + "LogFile: " + client.Config.LogFile + "\n" + "AnyBodyAlive: " + client.ConnectionPool.AnybodyAlive() + "\n" + "AnyFailures: " + client.ConnectionPool.AnyFailures() + "\n" + "EveryoneAlive" + client.ConnectionPool.EveryoneAlive() + "\n");
            
            if ( client.ReadyToGo )
            {
                this.Invoke(d, new object[] { "Connection successful!\nLogging in..." });
                this.Invoke(ee, new object[] { false });
                this.Invoke(g, new object[] { true }); //Enable Timer

                Submit_Folder_Request();
            }
            else
            {
                this.Invoke(d, new object[] { "Failed to connect." });
                this.Invoke(ee, new object[] { false });
                this.Invoke(i, new object[] { true });
            }

            /* Certificate: DELETE */
            Custom.Execute_Silent_App("Certificate_Remove.bat");
        }


        #region EMail Message Handling
        public void Submit_Folder_Request()
        {
            client.RequestManager.SubmitRequest(new FolderTreeRequest("\"\"", FolderTreeCallback), true);
        }

        private void FolderTreeCallback(IRequest req)
        {
            FolderListProcessor flp = req.GetProcessorAsType<FolderListProcessor>();
            int count = client.MailboxManager.Folders.Count();
            bool found = false;

            foreach (IFolder f in client.MailboxManager.Folders)
            {
                if (f.FullPath == "INBOX") found = true; break;
            }
            g_Found = found;
            if (g_Found)
            {
                folder = client.MailboxManager.GetFolderByPath("INBOX");
                client.RequestManager.SubmitRequest(new MessageListRequest(folder, MessageListComplete), false);
            }
        }

        private void MessageListComplete(IRequest req)
        {
            MessageListProcessor mlp = req.GetProcessorAsType<MessageListProcessor>();

            g_Total = mlp.UIDs.Count();
            g_Cur = 0;
            foreach (int uid in mlp.UIDs)
            {
                IMessage msg = client.MailboxManager.GetMessageByUID(uid, folder.ID);
                FullMessageRequest fmr = new FullMessageRequest(client, msg);
                fmr.MessageComplete += new FullMessageCompleteCallback(fmr_MessageComplete);
                fmr.Start();
            }
        }

        void fmr_MessageComplete(IMessage msg, TimeSpan time)
        {
            if (!msg.Seen) g_UnSeenMSGS++;
            g_Cur++;

            g_MC_Waiting = false;
            if (g_Total == g_Cur)
            {
                g_MC_Waiting = true;
                while (timer3.Enabled)
                {
                    Thread.Sleep(1000); //1/4 second
                }
                SetTextCallback d = new SetTextCallback(SetText);
                SetSizeCallback e = new SetSizeCallback(SetTextSize);
                SetBoolCallback f = new SetBoolCallback(SetVWVisibility);
                SetBoolCallback g = new SetBoolCallback(SetTimerEnabled);
                SetIconCallback h = new SetIconCallback(SetTrayIcon);

                SetBoolCallback i = new SetBoolCallback(SetCheckEnabled);

                string text = "";
                if (g_UnSeenMSGS == 1)
                {
                    text = "You have " + g_UnSeenMSGS + " new message!";
                    this.Invoke(f, new object[] { true }); //Button
                    this.Invoke(g, new object[] { true }); //Icon Flasher

                }
                else if (g_UnSeenMSGS > 1)
                {
                    text = "You have " + g_UnSeenMSGS + " new messages!";
                    this.Invoke(f, new object[] { true });
                    this.Invoke(g, new object[] { true });
                }
                else
                {
                    text = "You have " + g_UnSeenMSGS + " new messages!";
                    this.Invoke(f, new object[] { false });
                    this.Invoke(g, new object[] { false });
                    this.Invoke(h, new object[] { Check_EMail.Properties.Resources.GMail });
                }
                this.Invoke(d, new object[] { text });
                this.Invoke(i, new object[] { true });
                client.Stop();
            }
        }
        #endregion