コード例 #1
0
        public MechanismResponse Attempt(string domain)
        {
            MechanismResponse returnVal = new MechanismResponse();

            returnVal.Origin = _type;

            string xmlPath = string.Format(_format, domain);
            string xml     = WebClientGetXml(xmlPath);

            if (!string.IsNullOrEmpty(xml))
            {
                try
                {
                    returnVal.ClientConfig = Deserialize <ClientConfig>(xml);
                    returnVal.ResponseType = MechanismResponseType.Success;
                }
                catch (Exception ex)
                {
                    returnVal.Exception    = ex;
                    returnVal.ResponseType = MechanismResponseType.Exception;
                }
            }
            else
            {
                returnVal.ResponseType = MechanismResponseType.NotFound;
            }

            return(returnVal);
        }
コード例 #2
0
        private static MechanismResponse GetAutoconfigByDomain(string domain, RequestType requestType)
        {
            MechanismResponse returnVal = new MechanismResponse();

            if (!string.IsNullOrEmpty(domain))
            {
                List <Mechanism> mechanisms = GetMechanisms();

                switch (requestType)
                {
                case RequestType.Standard:
                    returnVal = AttemptAll(mechanisms, domain);
                    break;

                case RequestType.MxLookup:
                    bool     found     = false;
                    string[] mxRecords = MxLookupHandler.GetMXRecordsTrimDistinct(domain);

                    if (mxRecords != null && mxRecords.Length > 0)
                    {
                        foreach (string mx in mxRecords)
                        {
                            returnVal = AttemptAll(mechanisms, mx);
                            found     = returnVal != null && returnVal.IsSuccess;
                            if (found)
                            {
                                break;
                            }
                        }

                        if (!found)     //Guess from the mx record
                        {
                            foreach (string mx in mxRecords)
                            {
                                returnVal = MxGuessHandler.GuessConfig(mx);
                                found     = returnVal != null && returnVal.IsSuccess;
                                if (found)
                                {
                                    break;
                                }
                            }
                        }
                    }
                    break;

                case RequestType.Guess:
                    returnVal = MxGuessHandler.GuessConfig(domain);
                    break;
                }
            }

            if (returnVal != null)
            {
                returnVal.RequestType = requestType;
            }

            return(returnVal);
        }
コード例 #3
0
        private static MechanismResponse AttemptAll(List <Mechanism> mechanisms, string domain)
        {
            MechanismResponse returnVal = new MechanismResponse();

            foreach (Mechanism mechanism in mechanisms)
            {
                returnVal = mechanism.Attempt(domain);
                if (returnVal != null && returnVal.IsSuccess)
                {
                    break;
                }
            }

            return(returnVal);
        }
コード例 #4
0
        public static MechanismResponse GuessConfig(string domain)
        {
            MechanismResponse returnVal = new MechanismResponse();

            Dictionary <string, int[]> guesses = GetGuesses();
            List <TimeOutSocket>       sockets = new List <TimeOutSocket>();

            foreach (string key in guesses.Keys)
            {
                string domainGuess = string.Format(key, domain);

                foreach (int port in guesses[key])
                {
                    sockets.Add(new TimeOutSocket(domainGuess, port));
                }
            }

            sockets.ForEach(socket => socket.Test(4000));

            System.Threading.Thread.Sleep(4100);

            List <TimeOutSocket> openSockets = sockets.FindAll(socket => socket.IsSuccess);

            if (openSockets.Count > 1)
            {
                ClientConfig config = BuildClientConfig(openSockets, domain);

                if (config.EmailProvider.IncomingServers.Count > 0 &&
                    config.EmailProvider.OutgoingServers.Count > 0)
                {
                    returnVal.ClientConfig = config;
                    returnVal.ResponseType = MechanismResponseType.Success;
                    returnVal.Origin       = MechanismOriginType.Guess;
                }
            }

            sockets.ForEach(socket => socket.Dispose());

            return(returnVal);
        }
コード例 #5
0
        /// <summary>
        /// Gets the autoconfig.
        /// </summary>
        /// <param name="emailAddress">The email address.</param>
        /// <param name="guess">if set to <c>true</c> [guess the config if all other mechanisms fail].</param>
        /// <returns></returns>
        public static MechanismResponse GetAutoconfig(string emailAddress, RequestType requestType)
        {
            //Ignore SSL certificate errors
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

            MechanismResponse returnVal = new MechanismResponse();

            if (!string.IsNullOrEmpty(emailAddress))
            {
                int atIndex = emailAddress.IndexOf(At);

                if (atIndex > 0)
                {
                    string domain = emailAddress.Substring(atIndex + 1);
                    returnVal = GetAutoconfigByDomain(domain, requestType);
                }
            }

            ServicePointManager.ServerCertificateValidationCallback = null;

            return(returnVal);
        }