private async Task FetchAllInboxEmail() { if (_isConnected) { CancelEmailFetching(); EnableControl = true; ButtonContent = ConstantValues.StartButtonText; _isConnected = false; BodyHtml = ConstantValues.AboutBlank; } else { _isConnected = true; ButtonContent = ConstantValues.StopButtonText; EnableControl = false; try { await _emailManager.Initialize(CurrentUser.Username, CurrentUser.Password, ServerName, PortNumber, SelectedServerType, SelectedEncryptionType); int itterations = _emailManager.GetNumberOfIterations(); for (int index = 0, emailIndex = 0; index < itterations; ++index) { var emails = await _emailManager.ReadAllMailsParallely(); if (emails != null && _isConnected) { while (emailIndex < emails.Count) { OnEmailReceived(emails[emailIndex]); ++emailIndex; } } } await _emailManager.KeepOnlyTwoConnectionAlive(); } catch (Limilabs.Client.IMAP.ImapResponseException) { if (_isConnected) { _dialog.ShowMessage(ConstantValues.LoginFailedDescription, ConstantValues.LoginFailedTitle); ButtonContent = ConstantValues.StartButtonText; EnableControl = true; _isConnected = false; await _emailManager.CloseAllConnections(); } } catch (Limilabs.Client.ServerException) { if (_isConnected) { _dialog.ShowMessage(ConstantValues.ConnectionErrorDescription, ConstantValues.ConnectionErrorTitle); ButtonContent = ConstantValues.StartButtonText; EnableControl = true; _isConnected = false; await _emailManager.CloseAllConnections(); } } catch (Exception) { if (_isConnected) { _dialog.ShowMessage(ConstantValues.UnknowErrorDescription, ConstantValues.UnknowErrorTitle); ButtonContent = ConstantValues.StartButtonText; EnableControl = true; _isConnected = false; await _emailManager.CloseAllConnections(); } } } }