private async void RetriveMessages(bool initialization = false) { _timer?.Stop(); using (var connection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { connection.Connect(_config.Incoming.Host, _config.Incoming.Port); if (!connection.Connected) { Disconnect(); App.Current.Dispatcher.Invoke( delegate { DisplayDialog("Błąd", "Nie udało się nawiązać połączenia z serwerem"); } ); return; } Status = "Połączono"; StatusColor = Brushes.Green; StreamReader reader; StreamWriter writer; if (_config.Incoming.SSL) { var ssl = new SslStream(new NetworkStream(connection)); try { ssl.AuthenticateAsClient(_config.Incoming.Host); } catch { Disconnect(); App.Current.Dispatcher.Invoke( delegate { DisplayDialog("Błąd", "Wystąpił błąd z połączeniem SSL"); } ); return; } reader = new StreamReader(ssl); writer = new StreamWriter(ssl); } else { reader = new StreamReader(new NetworkStream(connection)); writer = new StreamWriter(new NetworkStream(connection)); } IsLoading = true; await Task.Run(() => { reader.ReadLine(); // Powitanie serwera // AUTHENTICATION mode writer.WriteLine($"USER {_config.Incoming.Username}"); writer.Flush(); reader.ReadLine(); // Potwierdzenie odbioru loginu writer.WriteLine($"PASS {_config.Incoming.Password}"); writer.Flush(); reader.ReadLine(); // potwierdzenie odbioru hasła // TRANSACTION mode writer.WriteLine("UIDL"); writer.Flush(); string strTemp; var line = 0; while ((strTemp = reader.ReadLine()) != null) { if (".".Equals(strTemp) || strTemp.IndexOf("-ERR") != -1) { break; } if (line != 0) { if (!Mails.Contains(new Mail(strTemp.Substring(0, strTemp.IndexOf(" ")), strTemp.Substring(strTemp.IndexOf(" ") + 1), false))) { System.Windows.Application.Current.Dispatcher.Invoke( delegate { Mails.Add(new Mail(strTemp.Substring(0, strTemp.IndexOf(" ")), strTemp.Substring(strTemp.IndexOf(" ") + 1), !initialization)); } ); AllMessagesCounter++; if (!initialization) { NewMessagesCounter++; } } } else { line++; } } writer.WriteLine("QUIT"); writer.Flush(); reader.ReadLine(); // Potwierdzenie rozłączenia sesji }); IsLoading = false; } _timer?.Start(); }