コード例 #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.
        /// </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);
                }
            }
        }
コード例 #3
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"));
            }
        }