/// <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); } }
/// <summary> /// Get the MX records for the specified domain name using the specified DNS server. /// </summary> /// <param name="address">The domain name.</param> /// <param name="host">The host name of the DNS server to use.</param> /// <param name="port">The port number of the DNS server to use.</param> /// <param name="timeout">The timeout in miliseconds.</param> /// <returns>A collection of Mx Records.</returns> public static MxRecordCollection GetMxRecords(string address, string host, int port, int timeout) { var mxRecords = new MxRecordCollection(); var query = new DnsQuery(IPAddress.Parse(host)) { RecursiveQuery = true, DnsServer = { Port = port }, Domain = address }; DnsAnswer answer = query.QueryServer(RecordType.MX, timeout); foreach (Answer entry in answer.Answers) { var mxRecord = (MXRecord)entry.Data; mxRecords.Add(mxRecord.Domain, mxRecord.Preference); } return(mxRecords); }
/// <summary> /// Get the MX records for the specified domain name using the specified DNS server. /// </summary> /// <param name="address">The domain name.</param> /// <param name="host">The host name of the DNS server to use.</param> /// <param name="port">The port number of the DNS server to use.</param> /// <param name="timeout">The timeout in miliseconds.</param> /// <returns>A collection of Mx Records.</returns> public static ActiveUp.Net.Mail.MxRecordCollection GetMxRecords(string address, string host, int port, int timeout) { MxRecordCollection mxRecords = new MxRecordCollection(); DnsQuery query = new DnsQuery(IPAddress.Parse(host)); query.RecursiveQuery = true; query.DnsServer.Port = port; query.Domain = address; DnsAnswer answer = query.QueryServer(RecordType.MX, timeout); foreach (DnsEntry entry in answer.Answers) { MXRecord mxRecord = (MXRecord)entry.Data; mxRecords.Add(mxRecord.Domain, mxRecord.Preference); } return(mxRecords); }
/// <summary> /// Get the MX records for the specified domain name using the specified DNS server. /// </summary> /// <param name="address">The domain name.</param> /// <param name="host">The host name of the DNS server to use.</param> /// <param name="port">The port number of the DNS server to use.</param> /// <param name="timeout">The timeout in miliseconds.</param> /// <returns>A collection of Mx Records.</returns> public static ActiveUp.Net.Mail.MxRecordCollection GetMxRecords(string address, string host, int port, int timeout) { MxRecordCollection mxRecords = new MxRecordCollection(); DnsQuery query = new DnsQuery(IPAddress.Parse(host)); query.RecursiveQuery = true; query.DnsServer.Port = port; query.Domain = address; DnsAnswer answer = query.QueryServer(RecordType.MX, timeout); foreach (DnsEntry entry in answer.Answers) { MXRecord mxRecord = (MXRecord)entry.Data; mxRecords.Add(mxRecord.Domain, mxRecord.Preference); } return mxRecords; }
/// <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); } } }
/// <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 (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) return false; else { try { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 0; smtp.ReceiveTimeout = 0; MxRecordCollection mxRecords = new MxRecordCollection(); try { mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); } catch { new System.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) { System.Console.WriteLine(ex.ToString()); #if !PocketPC System.Web.HttpContext.Current.Trace.Write("ActiveMail", ex.ToString()); #endif result = false; } } smtp.Disconnect(); return result; } catch { return false; } } }
/// <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 (!ActiveUp.Net.Mail.Validator.ValidateSyntax(address)) return false; else { string domain = address.Split('@')[1]; bool result; ActiveUp.Net.Mail.SmtpClient smtp = new ActiveUp.Net.Mail.SmtpClient(); smtp.SendTimeout = 15; smtp.ReceiveTimeout = 15; MxRecordCollection mxRecords = new MxRecordCollection(); try { #if !PocketPC mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain, dnsServers); #else mxRecords = ActiveUp.Net.Mail.Validator.GetMxRecords(domain); #endif } catch { new System.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; } }