static public bool Test(BaseProtocolClient ingoing_mail_client, MailServerSettings settings)
        {
            try
            {
                IAsyncResult async_res;
                switch (settings.EncryptionType)
                {
                    case EncryptionType.StartTLS:
                        async_res = ingoing_mail_client.BeginConnect(settings.Url, settings.Port, null);
                        break;
                    case EncryptionType.SSL:
                        async_res = ingoing_mail_client.BeginConnectSsl(settings.Url, settings.Port, null);
                        break;
                    default:
                        async_res = ingoing_mail_client.BeginConnect(settings.Url, settings.Port, null);
                        break;
                }

                if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT))
                    throw new ImapConnectionTimeoutException();

                if (settings.EncryptionType == EncryptionType.StartTLS)
                {
                    ingoing_mail_client.StartTLS(settings.Url);
                }

                if (settings.AuthenticationType == SaslMechanism.Login)
                {
                    ingoing_mail_client.Login(settings.AccountName, settings.AccountPass, "");
                }
                else
                {
                    async_res = ingoing_mail_client.BeginAuthenticate(settings.AccountName, settings.AccountPass, settings.AuthenticationType, null);
                }

                if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT))
                    throw new ImapConnectionTimeoutException();

                if (async_res.AsyncState == null)
                    throw new AuthenticationException("Auth failed. Check your settings.");

                string s_result = ingoing_mail_client.EndConnectSsl(async_res).ToLowerInvariant();

                if (s_result.IndexOf("success", StringComparison.Ordinal) == -1 &&
                    s_result.IndexOf("+", StringComparison.Ordinal) == -1 &&
                    s_result.IndexOf("ok", StringComparison.Ordinal) == -1)
                    throw new ImapConnectionException(s_result);

                return true;
            }
            finally
            {
                if (ingoing_mail_client.IsConnected)
                {
                    ingoing_mail_client.Disconnect();
                }
            }
        }
Пример #2
0
        public static bool Test(BaseProtocolClient ingoingMailClient, MailServerSettings settings)
        {
            try
            {
                var sResult = ingoingMailClient.Authorize(settings, settings.MailServerOperationTimeoutInMilliseconds);
                if (sResult.ToLower().IndexOf("success", StringComparison.Ordinal) == -1 &&
                    sResult.ToLower().IndexOf("+", StringComparison.Ordinal) == -1 &&
                    sResult.ToLower().IndexOf("ok", StringComparison.Ordinal) == -1)
                {
                    if (ingoingMailClient is Imap4Client)
                        throw new ImapConnectionException(sResult);
                    if(ingoingMailClient is Pop3Client)
                        throw new Pop3ConnectionException(sResult);
                    else
                        throw new SmtpConnectionException(sResult);
                }

                return true;
            }
            catch (TargetInvocationException exTarget)
            {
                if (ingoingMailClient is Imap4Client)
                        throw new ImapConnectionException(exTarget.InnerException.Message);
                    if(ingoingMailClient is Pop3Client)
                        throw new Pop3ConnectionException(exTarget.InnerException.Message);
                    else
                        throw new SmtpConnectionException(exTarget.InnerException.Message);
            }
            catch (TimeoutException)
            {
                if (ingoingMailClient is Imap4Client)
                    throw new ImapConnectionTimeoutException();
                if (ingoingMailClient is Pop3Client)
                    throw new Pop3ConnectionTimeoutException();
                else
                    throw new SmtpConnectionTimeoutException();
            }
            finally
            {
                if (ingoingMailClient.IsConnected)
                {
                    try
                    {
                        ingoingMailClient.Disconnect();
                    }
                    catch {}
                    
                }
            }
        }