public static bool Test(MailBox account)
        {
            var ingoing_client = account.Imap ? (BaseProtocolClient)MailClientBuilder.Imap() : MailClientBuilder.Pop();

            Test(ingoing_client, new MailServerSettings
            {
                Url                = account.Server,
                Port               = account.Port,
                AccountName        = account.Account,
                AccountPass        = account.Password,
                AuthenticationType = account.AuthenticationTypeIn,
                EncryptionType     = account.IncomingEncryptionType,
                MailServerOperationTimeoutInMilliseconds = 10000
            });

            var outgoing_client = MailClientBuilder.Smtp();

            Test(outgoing_client, new MailServerSettings
            {
                Url                = account.SmtpServer,
                Port               = account.SmtpPort,
                AccountName        = account.SmtpAccount,
                AccountPass        = account.SmtpPassword,
                AuthenticationType = account.AuthenticationTypeSmtp,
                EncryptionType     = account.OutcomingEncryptionType,
                MailServerOperationTimeoutInMilliseconds = 10000
            });

            return(true);
        }
        public static bool Test(MailBox account)
        {
            BaseProtocolClient ingoing_client = (account.Imap) ? (BaseProtocolClient)MailClientBuilder.Imap() : MailClientBuilder.Pop();

            MailServerHelper.Test(ingoing_client, new MailServerSettings
            {
                Url                = account.Server,
                Port               = account.Port,
                AccountName        = account.Account,
                AccountPass        = account.Password,
                AuthenticationType = account.AuthenticationTypeIn,
                EncryptionType     = account.IncomingEncryptionType
            });

            MailServerHelper.TestSmtp(new MailServerSettings
            {
                Url                = account.SmtpServer,
                Port               = account.SmtpPort,
                AccountName        = account.SmtpAccount,
                AccountPass        = account.SmtpPassword,
                AuthenticationType = account.AuthenticationTypeSmtp,
                EncryptionType     = account.OutcomingEncryptionType
            });

            return(true);
        }
 public static bool TryTestSmtp(MailServerSettings settings, out string last_error)
 {
     try
     {
         last_error = String.Empty;
         return(Test(MailClientBuilder.Smtp(), settings));
     }
     catch (Exception ex)
     {
         last_error = ex.Message;
         return(false);
     }
 }
Beispiel #4
0
 public static bool TryTestPop(MailServerSettings settings, out string lastError)
 {
     try
     {
         lastError = String.Empty;
         return Test(MailClientBuilder.Pop(), settings);
     }
     catch (Exception ex)
     {
         lastError = ex.Message;
         return false;
     }
 }
        static public bool TestSmtp(MailServerSettings settings)
        {
            var    smtp     = MailClientBuilder.Smtp();
            string s_result = String.Empty;

            try
            {
                IAsyncResult async_res;
                if (settings.EncryptionType == EncryptionType.None || settings.EncryptionType == EncryptionType.StartTLS)
                {
                    async_res = smtp.BeginConnect(settings.Url, settings.Port, null);

                    if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT))
                    {
                        throw new SmtpConnectionTimeoutException();
                    }

                    if (settings.AuthenticationType != SaslMechanism.None || settings.EncryptionType == EncryptionType.StartTLS)
                    {
                        smtp.SendEhloHelo();
                    }

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

                    if (settings.AuthenticationType != SaslMechanism.None)
                    {
                        s_result = smtp.Authenticate(settings.AccountName, settings.AccountPass, settings.AuthenticationType);
                    }
                }
                else
                {
                    async_res = smtp.BeginConnectSsl(settings.Url, settings.Port, null);

                    if (!async_res.AsyncWaitHandle.WaitOne(WAIT_TIMEOUT))
                    {
                        throw new SmtpConnectionTimeoutException();
                    }

                    if (settings.AuthenticationType != SaslMechanism.None)
                    {
                        s_result = smtp.Authenticate(settings.AccountName, settings.AccountPass, settings.AuthenticationType);
                    }
                }

                if (settings.AuthenticationType != SaslMechanism.None && !s_result.StartsWith("+"))
                {
                    throw new SmtpConnectionException(s_result);
                }

                return(true);
            }
            finally
            {
                if (smtp.IsConnected)
                {
                    smtp.Disconnect();
                }
            }
        }