Ejemplo n.º 1
0
        public void ReceiveMails(Object threadContext)
        {
            logger.Trace("Checking Mail");
            try
            {
                using (Pop3Client client = new Pop3Client())
                {
                    logger.Trace("Connect to Server");
                    client.Connect("pop.gmail.com", 995, true);

                    logger.Trace("Authenticate Username Password");
                    client.Authenticate(Variables.MyEmail, Variables.MyEmailPassword);

                    List <string>  Uids        = client.GetMessageUids();
                    List <Message> newMessages = new List <Message>();

                    // All the new messages not seen by the POP3 client
                    for (int i = 0; i < Uids.Count; i++)
                    {
                        string currentUidOnServer = Uids[i];
                        if (!Variables.seenUids.Contains(currentUidOnServer))
                        {
                            // We have not seen this message before.
                            // Download it and add this new uid to seen uids

                            // the uids list is in messageNumber order - meaning that the first
                            // uid in the list has messageNumber of 1, and the second has
                            // messageNumber 2. Therefore we can fetch the message using
                            // i + 1 since messageNumber should be in range [1, messageCount]
                            Message unseenMessage = client.GetMessage(i + 1);

                            // Add the message to the new messages
                            newMessages.Add(unseenMessage);

                            // Add the uid to the seen uids, as it has now been seen
                            Variables.seenUids.Add(currentUidOnServer);
                        }
                    }

                    //PURGE email, since openpop.net and gmail delete option is broken
                    //all email check by pepper automatically deleted and send it to
                    //THRASH on the web, for THIRTY DAYS (30 days) only. --BUGS

                    if (newMessages.Count == 0)
                    {
                        logger.Trace("No Mail");
                        Main._Main.ShowBalloonTip("No mails...");
                    }
                    else
                    {
                        logger.Trace("Got New {0} Mail(s)", newMessages.Count);
                        Main._Main.ShowBalloonTip("I got NEW mail!!!");
                        Array.Resize <ManualResetEvent>(ref Variables.MailReaders, newMessages.Count);
                        for (int i = 0; i < newMessages.Count; i++)
                        {
                            Variables.MailReaders[i] = new ManualResetEvent(false);
                            NewMail NM = new NewMail(Variables.MailReaders[i], newMessages[i]);
                            ThreadPool.QueueUserWorkItem(NM.ReadMail);
                        }
                    }
                    //FileSystem.saveUIDs(Uids); no need  to save also...
                }
            }
            catch (PopServerNotFoundException)
            {
                Main._Main.ShowBalloonTip("No internet!");
                //MessageBox.Show(this, "Error occurred retrieving mail. " + e.Message, "POP3 Retrieval");
            }
            catch (Exception e)
            {
                Main._Main.ShowBalloonTip("Error occurred retrieving mail. " + e.Message);
                //MessageBox.Show(this, "Error occurred retrieving mail. " + e.Message, "POP3 Retrieval");
            }
            finally
            {
                _doneEvent.Set();
            }
        }