public void ConnectAndLogin() { if(_connection != null) { this.Dispose(); } _connection = getClientObject(); try { if (_encryption == EncryptionType.Unencrypted) { _connection.Connect(this._server, this._port); } else { _connection.ConnectSSL(this._server, this._port); if (_encryption == EncryptionType.STARTTLS) startTSL(); } login(_username, _password); } catch(ImapResponseException ire) { //aditional authentication by gmail if (ire.Message.StartsWith(GMAIL_LOGINONBROWSER_MESSAGE, StringComparison.InvariantCultureIgnoreCase) && _server.EndsWith(GMAIL_DOMAIN, StringComparison.InvariantCultureIgnoreCase)) { //tried to open the gmail authentication page, but it doesn't help //if (ire.Response.Lines.Count > 0) //{ // System.Diagnostics.Process.Start(ire.Response.Lines[0].Substring(ire.Response.Lines[0].IndexOf("http"))); // StatusProvider.DisplayStatus("Please authenticate on the opened web page, and try again."); //} //switching "unsafe authentication" is needed in gmail settings, so displaying the support page describing it var httpPos = ire.Message.IndexOf("http"); if(httpPos>-1) { var url = ire.Message.Substring(httpPos); var spacePos = url.IndexOf(" "); if (spacePos > -1) url = url.Substring(0, spacePos); System.Diagnostics.Process.Start(url); ErrorProvider.DisplayError("Gmail authentication failed, please read the opened support page."); } } else throw new MailServerException(ire.Message); } catch(ServerException se) { throw new MailServerException(se.Message); } }