private void Auth()
 {
     try
     {
         GlobalSettings.LoadSettings();
     }
     catch //(Exception ex)
     {
         //MessageBox.Show(ex.ToString());
     }
     var allUsers = clientDB.DisplayAllUsers();
     if (allUsers.Count != 0)
     {
         var clAllAcs = clientDB.AllAccount();
         myCon = new Imap(Cryptography.Decrypt(allUsers[0]), Cryptography.Decrypt(clAllAcs[allUsers[0]]));
         try
         {
             myCon.Connection();
             if (firstLogin)
             {
                 Tray myTray = new Tray();
                 firstLogin = false;
             }
         }
         catch (Exception ex)
         {
             //MessageBox.Show(ex.ToString());
             this.Show();
         }
     }
     else
     {
         this.Show();
     }
 }
 private void buttonLogin_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         bool CorrectLogin = (textBoxLogin.Text.Length > 5 && textBoxLogin.Text.Length < 50 && textBoxLogin.Text.Contains('@')),
             CorrectPassword = (textBoxPassword.Password.Length > 3 && textBoxPassword.Password.Length < 50);
         int count = -1;
         if (CorrectLogin && CorrectPassword)
         {
             try
             {
                 myCon = new Imap(textBoxLogin.Text, textBoxPassword.Password);
                 myCon.Connection();
                 count = myCon.Connections.Inbox.Count;
             }
             catch (Exception ex)
             {
                 //MessageBox.Show(ex.ToString());
                 MessageBox.Show("Подключение не удалось. Проверьте правильность данных и наличие интернет соединения.");
             }
         }
         else
         {
             MessageBox.Show("Ваши данные некорректны");
         }
         if (Edit)
         {
             clientDB.UpdateEmail(Cryptography.Encrypt(OldEmail), Cryptography.Encrypt(textBoxLogin.Text), Cryptography.Encrypt(textBoxPassword.Password));
             this.Close();
         }
         else
         {
             bool NeedIt = true;
             if (count != -1)
             {
                 if (clientDB.DisplayAllUsers().Count != 0)
                 {
                     if (clientDB.DisplayAllUsers().Contains(Cryptography.Encrypt(textBoxLogin.Text)))
                         NeedIt = false;
                 }
                 if (NeedIt)
                 {
                     clientDB.InsertUser(Cryptography.Encrypt(textBoxLogin.Text), Cryptography.Encrypt(textBoxPassword.Password), true);
                     if (firstLogin)
                     {
                         this.Hide();
                         Tray myTray = new Tray();
                         firstLogin = false;
                     }
                     else
                         this.Close();
                 }
                 else
                 {
                     MessageBox.Show("Такой адрес уже есть");
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
        //Эвент таймера, тут надо показывать зулины сообщения
        private void timerTick(object sender, EventArgs e)
        {
            try
            {
                SetServerCheckInterval();
                //MessageBox.Show(MessagesToShow.Count.ToString());
                foreach (string tempStr in MessagesToShow.Keys)
                {
                    List<String> address = new List<string>(),
                        name = new List<string>(),
                        theme = new List<string>(),
                        text = new List<string>();
                   // MessageBox.Show(Cryptography.Decrypt(tempStr) + "  " + MessagesToShow[tempStr].Count.ToString());
                    int count = 0;
                    foreach (MimeMessage m in MessagesToShow[tempStr])
                    {
                        string from = m.From[0].ToString(),
                        addresFrom = from.Split(' ').Last();
                        address.Add(addresFrom.Substring(1, addresFrom.Length - 1));
                        name.Add(m.From[0].Name);
                        if (m.Subject != null)
                            theme.Add(m.Subject);
                        else
                            theme.Add("<Без темы>");
                        text.Add(m.TextBody);
                        count++;
                        //MessageBox.Show(Cryptography.Decrypt(tempStr) + " " + m.Subject);
                    }
                    MessagesToShow[tempStr].Clear();
                    //MessagesToShow[tempStr].Clear();
                    //MessageBox.Show(Cryptography.Decrypt(tempStr) + " " + count);
                    if (count > 0)
                    {
                        ShowToaster(text, address, name, theme, Cryptography.Decrypt(tempStr));
                        address.Clear();
                        name.Clear();
                        theme.Clear();
                        text.Clear();
                    }
                    //MessagesToShow.Remove(tempStr);
                }               

                var actAccs = clientDB.ActiveAccount();
                var actUsers = clientDB.DisplayActiveUsers();
                var tempCount = clientDB.GetAccountMessages();

                int UsersCount = actUsers.Count;

                Task[] TaskArr = new Task[UsersCount];
                MessagesToShow = new Dictionary<string, List<MimeMessage>>();
                //MessageBox.Show("Количество задач: " +TaskArr.Length.ToString());
                for (int i = 0; i < UsersCount; i++)
                {
                    int j = i;
                    TaskArr[i] = new Task(() =>
                        {
                            try
                            {
                                Imap NewImap = new Imap(Cryptography.Decrypt(actUsers[j]), Cryptography.Decrypt(actAccs[actUsers[j]]));
                                NewImap.Connection();
                                //MessageBox.Show(NewImap.currentEmail + " Стратортавала задача");
                                if (tempCount[actUsers[j]] != -1)
                                {
                                    int NewMessagesCount = NewImap.Connections.Inbox.Count;
                                    List<MimeMessage> NewList = new List<MimeMessage>();
                                    while (NewMessagesCount > tempCount[actUsers[j]])
                                    {
                                        NewList.Add(NewImap.DownloadMessage(tempCount[actUsers[j]]));
                                        tempCount[actUsers[j]]++;
                                        clientDB.UpdateAccountMessages(actUsers[j], tempCount[actUsers[j]]);
                                    }
                                    if (!MessagesToShow.ContainsKey(actUsers[j]))
                                    {
                                        MessagesToShow.Add(actUsers[j], NewList);
                                    }
                                    else
                                    {
                                        MessagesToShow[actUsers[j]].AddRange(NewList);
                                    }                                    
                                }
                                else
                                {
                                    clientDB.UpdateAccountMessages(Cryptography.Encrypt(NewImap.currentEmail), NewImap.Connections.Inbox.Count);
                                }
                                NewImap.Disconnection();
                            }
                            catch //(Exception ex)
                            {
                                //MessageBox.Show(ex.ToString());
                            }
                        });
                }
                foreach (Task task in TaskArr)
                {
                    task.Start();
                }
                //Task.WaitAll(TaskArr);
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                MessageBox.Show("Не удалось загрузить письмо. Уведомления остановлены");
                stopTimer((object)tbiField.ContextMenu.Items[1], new RoutedEventArgs());
            }
        }