示例#1
0
 public static bool TryTestPop(MailServerSettings settings, out string last_error)
 {
     try
     {
         last_error = String.Empty;
         return(Test(MailClientBuilder.Pop(), settings));
     }
     catch (Exception ex)
     {
         last_error = ex.Message;
         return(false);
     }
 }
示例#2
0
 static public bool TryTestSmtp(MailServerSettings settings, out string last_error)
 {
     try
     {
         last_error = String.Empty;
         return(TestSmtp(settings));
     }
     catch (Exception ex)
     {
         last_error = ex.Message;
         return(false);
     }
 }
 static public bool TryTestSmtp(MailServerSettings settings, out string last_error)
 {
     try
     {
         last_error = String.Empty;
         return TestSmtp(settings);
     }
     catch (Exception ex)
     {
         last_error = 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 SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);

                    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 SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);

                    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();
            }
        }
示例#5
0
        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 SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);
                    }

                    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 SmtpConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);
                    }

                    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();
                }
            }
        }
示例#6
0
        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 ImapConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);
                }

                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 ImapConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);
                }

                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();
                }
            }
        }
        public int SaveMailServerSettings(MailAddress email, MailServerSettings settings, string server_type, 
            AuthorizationServiceType authorization_type)
        {
            var host = (authorization_type == AuthorizationServiceType.Google) ? GoogleHost : email.Host; 

            using (var db = GetDb())
            {
                var provider_id = db.ExecuteScalar<int>(new SqlQuery(MAIL_MAILBOX_DOMAIN)
                                                            .Select(MailBoxDomainFields.id_provider)
                                                            .Where(MailBoxProviderFields.name, host));

                //Save Mailbox provider if not exists
                if (provider_id == 0)
                {
                    provider_id = db.ExecuteScalar<int>(new SqlInsert(MAIL_MAILBOX_PROVIDER)
                                                            .InColumnValue(MailBoxProviderFields.id, 0)
                                                            .InColumnValue(MailBoxProviderFields.name, email.Host)
                                                            .Identity(0, 0, true));
                    db.ExecuteNonQuery(new SqlInsert(MAIL_MAILBOX_DOMAIN)
                                                            .InColumnValue(MailBoxDomainFields.id_provider, provider_id)
                                                            .InColumnValue(MailBoxDomainFields.name, email.Host));
                }

                //Identify mask for account name
                var account_name_mask = "";
                if (settings.AuthenticationType != SaslMechanism.None)
                {
                    account_name_mask = GetLoginFormatFrom(email, settings.AccountName);
                    if (String.IsNullOrEmpty(account_name_mask))
                    {
                        account_name_mask = settings.AccountName;
                    }
                }

                var settings_id = db.ExecuteScalar<int>(new SqlQuery(MAIL_MAILBOX_SERVER)
                    .Select(MailBoxServerFields.id)
                    .Where(MailBoxServerFields.id_provider, provider_id)
                    .Where(MailBoxServerFields.type, server_type)
                    .Where(MailBoxServerFields.hostname, settings.Url)
                    .Where(MailBoxServerFields.port, settings.Port)
                    .Where(MailBoxServerFields.socket_type,
                           ConvertFromEncryptionType(settings.EncryptionType))
                     .Where(MailBoxServerFields.authentication,
                                    ConvertFromSaslMechanism(settings.AuthenticationType))
                     .Where(MailBoxServerFields.username, account_name_mask)
                     .Where(MailBoxServerFields.is_user_data, false));

                if (settings_id == 0)
                {
                    settings_id = db.ExecuteScalar<int>(new SqlInsert(MAIL_MAILBOX_SERVER)
                                           .InColumnValue(MailBoxServerFields.id, 0)
                                           .InColumnValue(MailBoxServerFields.id_provider, provider_id)
                                           .InColumnValue(MailBoxServerFields.type, server_type)
                                           .InColumnValue(MailBoxServerFields.hostname, settings.Url)
                                           .InColumnValue(MailBoxServerFields.port, settings.Port)
                                           .InColumnValue(MailBoxServerFields.socket_type,
                                                          ConvertFromEncryptionType(settings.EncryptionType))
                                           .InColumnValue(MailBoxServerFields.authentication,
                                                          ConvertFromSaslMechanism(settings.AuthenticationType))
                                           .InColumnValue(MailBoxServerFields.username, account_name_mask)
                                           .InColumnValue(MailBoxServerFields.is_user_data, true)
                                           .Identity(0,0, true));
                }

                return settings_id;
            }
        }
        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 ImapConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);

                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 ImapConnectionException(MailQueueItem.CONNECTION_TIMEOUT_ERROR);

                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();
                }
            }
        }
 public static bool TryTestPop(MailServerSettings settings, out string last_error)
 {
     try
     {
         last_error = String.Empty;
         return Test(MailClientBuilder.Pop(), settings);
     }
     catch (Exception ex)
     {
         last_error = ex.Message;
         return false;
     }
 }