Extended error message on Imapbase.Connect

Dec 16, 2015 at 9:44 AM
Hi all,

I had some trouble connecting the imap server in our environment. This was not a problem of imapX, but the ImapClient.Connect method does not return any inner error message.
So my suggestion is to give ImapBase.Connect an additionally parameter (bool throwExceptionOnError = false) so the caller can decide if he wants the full exception if something goes wrong.
/// <summary>
///     Connects to an IMAP server on the specified port
/// </summary>
/// <param name="host">Server address</param>
/// <param name="port">Server port</param>
/// <param name="sslProtocol">SSL protocol to use, <code>SslProtocols.None</code> by default</param>
/// <param name="validateServerCertificate">Defines whether the server certificate should be validated when SSL is used</param>
/// <param name="throwExceptionOnError">Rethrows the exception in case of problems. This helps to analyze connections problems</param>
/// <returns><code>true</code> if the connection was successful</returns>
/// <exception cref="Exceptions.InvalidStateException">If the client is already connected.</exception>
public bool Connect(string host, int port, SslProtocols sslProtocol = SslProtocols.None,
    bool validateServerCertificate = true, bool throwExceptionOnError = false)
{
    _host = host;
    _port = port;
    _sslProtocol = sslProtocol;
    _validateServerCertificate = validateServerCertificate;

    if (IsConnected)
        throw new InvalidStateException("The client is already connected. Please disconnect first.");

    try
    {
#if !WINDOWS_PHONE && !NETFX_CORE
        _client = new TcpClient(_host, _port);

        if (_sslProtocol == SslProtocols.None)
        {
            _ioStream = _client.GetStream();
            _streamReader = new StreamReader(_ioStream);
        }
        else
        {
            _ioStream = new SslStream(_client.GetStream(), false, CertificateValidationCallback, null);
            (_ioStream as SslStream).AuthenticateAsClient(_host, null, _sslProtocol, false);
            _streamReader = new StreamReader(_ioStream);
        }
#else
//TODO: Add support for Tls
        _client = _sslProtocol == SslProtocols.None ? new TcpClient(_host, _port) : new SecureTcpClient(_host, _port);
        _ioStream = _client.GetStream();
        _streamReader = new StreamReader(_ioStream);
#endif

        string result = _streamReader.ReadLine();

        _lastActivity = DateTime.Now;

        if (result != null && result.StartsWith(ResponseType.ServerOk))
        {
            Capability();
            return true;
        }
        else if (result != null && result.StartsWith(ResponseType.ServerPreAuth))
        {
            IsAuthenticated = true;
            Capability();
            return true;
        }
        else
            return false;
    }
    catch (Exception)
    {
        if (throwExceptionOnError)
            throw;
        return false;
    }
    finally
    {
        if (!IsConnected)
            CleanUp();
    }
}
Regards

Joerg