Пример #1
0
        /// <summary>
        /// Validates syntax and existence of the given address.
        /// </summary>
        /// <param name="address">The address to be validated.</param>
        /// <param name="dnsServers">Name Servers to be used for MX records search.</param>
        /// <returns>True if the address is valid, otherwise false.</returns>
        public static bool Validate(string address, ServerCollection dnsServers)
        {
            if (!ValidateSyntax(address))
            {
                return(false);
            }
            else
            {
                string     domain = address.Split('@')[1];
                bool       result;
                SmtpClient smtp = new SmtpClient();
                smtp.SendTimeout    = 15;
                smtp.ReceiveTimeout = 15;

                MxRecordCollection mxRecords = new MxRecordCollection();
                try
                {
#if !PocketPC
                    mxRecords = GetMxRecords(domain, dnsServers);
#else
                    mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain);
#endif
                }
                catch
                {
                    new Exception("Can't connect to DNS server.");
                }
                smtp.Connect(mxRecords.GetPrefered().Exchange);
                try
                {
                    smtp.Ehlo(System.Net.Dns.GetHostName());
                }
                catch
                {
                    smtp.Helo(System.Net.Dns.GetHostName());
                }
                if (smtp.Verify(address))
                {
                    result = true;
                }
                else
                {
                    try
                    {
                        //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName());
                        //smtp.MailFrom("*****@*****.**");
                        smtp.MailFrom("postmaster@" + domain);
                        smtp.RcptTo(address);
                        result = true;
                    }
                    catch
                    {
                        result = false;
                    }
                }
                smtp.Disconnect();
                return(result);
            }
        }
Пример #2
0
        /// <summary>
        /// Validates syntax and existence of the given address and returns valid addresses.
        /// </summary>
        /// <param name="addresses">The collection to be filtered.</param>
        /// <param name="dnsServers">Name Servers to be used for MX records search.</param>
        /// <returns>A collection containing the valid addresses.</returns>
        public static AddressCollection Filter(AddressCollection addresses, ServerCollection dnsServers)
        {
            AddressCollection valids  = new AddressCollection();
            AddressCollection valids1 = new AddressCollection();

            System.Collections.Specialized.HybridDictionary ads = new System.Collections.Specialized.HybridDictionary();
            for (int i = 0; i < addresses.Count; i++)
            {
                if (ValidateSyntax(addresses[i].Email))
                {
                    valids.Add(addresses[i]);
                }
            }
#if !PocketPC
            Array domains = Array.CreateInstance(typeof(string), new int[] { valids.Count }, new int[] { 0 });
            Array adds    = Array.CreateInstance(typeof(Address), new int[] { valids.Count }, new int[] { 0 });
#else
            System.Array domains = System.Array.CreateInstance(typeof(string), new int[] { valids.Count });
            System.Array adds    = System.Array.CreateInstance(typeof(ActiveUp.Net.Mail.Address), new int[] { valids.Count });
#endif
            for (int i = 0; i < valids.Count; i++)
            {
                domains.SetValue(valids[i].Email.Split('@')[1], i);
                adds.SetValue(valids[i], i);
            }
            Array.Sort(domains, adds, null);
            string     currentDomain = "";
            string     address       = "";
            SmtpClient smtp          = new SmtpClient();
            bool       isConnected   = false;
            for (int i = 0; i < adds.Length; i++)
            {
                address = ((Address)adds.GetValue(i)).Email;
                if (((string)domains.GetValue(i)) == currentDomain)
                {
                    if (!smtp.Verify(address))
                    {
                        try
                        {
                            //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName());
                            //smtp.MailFrom("postmaster@"+currentDomain);
                            smtp.RcptTo(address);
                            valids1.Add((Address)adds.GetValue(i));
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        valids1.Add((Address)adds.GetValue(i));
                    }
                }
                else
                {
                    currentDomain = (string)domains.GetValue(i);
                    try
                    {
                        if (isConnected == true)
                        {
                            isConnected = false;
                            smtp.Disconnect();
                            smtp = new SmtpClient();
                        }

                        smtp.Connect(GetMxRecords(currentDomain, dnsServers).GetPrefered().Exchange);
                        isConnected = true;
                        try
                        {
                            smtp.Ehlo(System.Net.Dns.GetHostName());
                        }
                        catch
                        {
                            smtp.Helo(System.Net.Dns.GetHostName());
                        }
                        if (!smtp.Verify(address))
                        {
                            try
                            {
                                //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName());
                                //smtp.MailFrom("*****@*****.**");
                                smtp.MailFrom("postmaster@" + currentDomain);
                                smtp.RcptTo(address);
                                valids1.Add((Address)adds.GetValue(i));
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            valids1.Add((Address)adds.GetValue(i));
                        }
                    }
                    catch
                    {
                    }
                }
            }
            if (isConnected == true)
            {
                smtp.Disconnect();
            }
            return(valids1);
        }
Пример #3
0
        /// <summary>
        /// Validates syntax and existence of the given address.
        /// </summary>
        /// <param name="address">The address to be validated.</param>
        /// <returns>True if the address is valid, otherwise false.</returns>
        public static bool Validate(string address)
        {
            if (!ValidateSyntax(address))
            {
                return(false);
            }
            else
            {
                try
                {
                    string     domain = address.Split('@')[1];
                    bool       result;
                    SmtpClient smtp = new SmtpClient();
                    smtp.SendTimeout    = 0;
                    smtp.ReceiveTimeout = 0;
                    MxRecordCollection mxRecords = new MxRecordCollection();
                    try
                    {
                        mxRecords = GetMxRecords(domain);
                    }
                    catch
                    {
                        new Exception("Can't connect to DNS server.");
                    }
                    //Console.WriteLine(mxRecords.GetPrefered().Exchange);
                    if (mxRecords.Count > 0)
                    {
                        smtp.Connect(mxRecords.GetPrefered().Exchange);
                    }
                    else
                    {
                        return(false);
                    }
                    try
                    {
                        smtp.Ehlo(System.Net.Dns.GetHostName());
                    }
                    catch
                    {
                        smtp.Helo(System.Net.Dns.GetHostName());
                    }
                    if (smtp.Verify(address))
                    {
                        result = true;
                    }
                    else
                    {
                        try
                        {
                            //smtp.MailFrom("postmaster@"+System.Net.Dns.GetHostName());
                            //smtp.MailFrom("*****@*****.**");
                            smtp.MailFrom("postmaster@" + domain);
                            smtp.RcptTo(address);
                            result = true;
                        }
                        catch (Exception ex)
                        {
#if (DEBUG)
                            Console.WriteLine(ex.ToString());
#endif
#if !PocketPC
                            HttpContext.Current.Trace.Write("ActiveMail", ex.ToString());
#endif
                            result = false;
                        }
                    }
                    smtp.Disconnect();
                    return(result);
                }
                catch
                {
                    return(false);
                }
            }
        }
Пример #4
0
        public ValidationResult Validate(string address)
        {
            if (!ValidateSyntax(address))
            {
                return(ValidationResult.Error("Incorrect email syntax"));
            }

            try
            {
                var address1 = address.Split('@')[1];
                using var smtpClient = new SmtpClient { SendTimeout = 0, ReceiveTimeout = 0 };
                var recordCollection = new MxRecordCollection();
                try
                {
                    recordCollection = Validator.GetMxRecords(address1);
                }
                catch
                {
                    _logger.LogInformation("Can't connect to DNS server.");
                }

                if (recordCollection.Count <= 0)
                {
                    return(ValidationResult.Error("Incorrect dns"));
                }

                smtpClient.Connect(recordCollection.GetPrefered().Exchange);
                try
                {
                    smtpClient.Ehlo(Dns.GetHostName());
                }
                catch
                {
                    smtpClient.Helo(Dns.GetHostName());
                }

                ValidationResult flag;
                if (smtpClient.Verify(address))
                {
                    flag = ValidationResult.Success;
                }
                else
                {
                    try
                    {
                        smtpClient.MailFrom("postmaster@" + address1);
                        smtpClient.RcptTo(address);
                        flag = ValidationResult.Success;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogInformation(ex.ToString());
                        flag = ValidationResult.Error("Email doest not exist");
                    }
                }

                smtpClient.Disconnect();
                return(flag);
            }
            catch
            {
                return(ValidationResult.Error("Unknown error"));
            }
        }