protected void Button1_Click(object sender, System.EventArgs e) { emailvalidator1.ValidationLevel = (EmailValidationLevel)StringUtils.StrToIntDef(cbLevel.Text, 0); emailvalidator1.DnsServer = edtDNS.Text; emailvalidator1.BlackList = StringUtils.GetStringArray(edtBlackList.Text); EmailValidationResult result = emailvalidator1.Validate(edtEmail.Text); switch (result) { case EmailValidationResult.BlacklistOk: edtResult.Text = "Blacklist passed"; break; case EmailValidationResult.SyntaxOk: edtResult.Text = "Syntax valid"; break; case EmailValidationResult.DomainOk: edtResult.Text = "Domain valid"; break; case EmailValidationResult.SmtpOk: edtResult.Text = "Email exchanger valid"; break; case EmailValidationResult.MailboxOk: edtResult.Text = "Mailbox valid"; break; default: edtResult.Text = "E-mail invalid"; break; } }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <param name="result"></param> /// <returns></returns> /// <exception cref="EmailValidatorException"></exception> public bool Validate(string email, out EmailValidationResult result) { if (string.IsNullOrWhiteSpace(email)) { result = EmailValidationResult.AddressIsEmpty; return true; } email = email.Trim(); MailAddress mailAddress = null; try { mailAddress = new MailAddress(email); } catch (ArgumentNullException e) { result = EmailValidationResult.AddressIsEmpty; return true; } catch (ArgumentException e) { result = EmailValidationResult.AddressIsEmpty; return true; } catch (FormatException e) { result = EmailValidationResult.InvalidFormat; return true; } if (mailAddress.Address != email) { result = EmailValidationResult.InvalidFormat; return true; } ////////////////// DomainName domainName = DomainName.Parse(mailAddress.Host); DnsMessage dnsResponse = DnsClient.Default.Resolve(domainName, RecordType.Mx); IList<MxRecord> mxRecords = dnsResponse.AnswerRecords.OfType<MxRecord>().ToList(); if (mxRecords.Count == 0) { result = EmailValidationResult.NoMailForDomain; return true; } foreach (MxRecord mxRecord in mxRecords) { try { SmtpClient smtpClient = new SmtpClient(mxRecord.ExchangeDomainName.ToString()); SmtpStatusCode resultCode; if (smtpClient.CheckMailboxExists(email, out resultCode)) { switch (resultCode) { case SmtpStatusCode.Ok: result = EmailValidationResult.OK; return true; case SmtpStatusCode.ExceededStorageAllocation: result = EmailValidationResult.MailboxStorageExceeded; return true; case SmtpStatusCode.MailboxUnavailable: result = EmailValidationResult.MailboxUnavailable; return true; } } } catch (SmtpClientException) { } catch (ArgumentNullException) { } } if (mxRecords.Count > 0) { result = EmailValidationResult.MailServerUnavailable; return false; } result = EmailValidationResult.Undefined; return false; }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <param name="result"></param> /// <returns></returns> /// <exception cref="EmailValidatorException"></exception> public bool Validate(string email, out EmailValidationResult result) { if (string.IsNullOrWhiteSpace(email)) { result = EmailValidationResult.AddressIsEmpty; return(true); } email = email.Trim(); MailAddress mailAddress = null; try { mailAddress = new MailAddress(email); } catch (ArgumentNullException e) { result = EmailValidationResult.AddressIsEmpty; return(true); } catch (ArgumentException e) { result = EmailValidationResult.AddressIsEmpty; return(true); } catch (FormatException e) { result = EmailValidationResult.InvalidFormat; return(true); } if (mailAddress.Address != email) { result = EmailValidationResult.InvalidFormat; return(true); } ////////////////// DomainName domainName = DomainName.Parse(mailAddress.Host); DnsMessage dnsResponse = DnsClient.Default.Resolve(domainName, RecordType.Mx); IList <MxRecord> mxRecords = dnsResponse.AnswerRecords.OfType <MxRecord>().ToList(); if (mxRecords.Count == 0) { result = EmailValidationResult.NoMailForDomain; return(true); } foreach (MxRecord mxRecord in mxRecords) { try { SmtpClient smtpClient = new SmtpClient(mxRecord.ExchangeDomainName.ToString()); SmtpStatusCode resultCode; if (smtpClient.CheckMailboxExists(email, out resultCode)) { switch (resultCode) { case SmtpStatusCode.Ok: result = EmailValidationResult.OK; return(true); case SmtpStatusCode.ExceededStorageAllocation: result = EmailValidationResult.MailboxStorageExceeded; return(true); case SmtpStatusCode.MailboxUnavailable: result = EmailValidationResult.MailboxUnavailable; return(true); } } } catch (SmtpClientException) { } catch (ArgumentNullException) { } } if (mxRecords.Count > 0) { result = EmailValidationResult.MailServerUnavailable; return(false); } result = EmailValidationResult.Undefined; return(false); }
/// <summary> /// /// </summary> /// <param name="email"></param> /// <param name="result"></param> /// <returns></returns> /// <exception cref="EmailValidatorException"></exception> public bool Validate(string email, out EmailValidationResult result) { if (string.IsNullOrWhiteSpace(email)) { result = EmailValidationResult.AddressIsEmpty; return(true); } email = email.Trim(); MailAddress mailAddress = null; try { mailAddress = new MailAddress(email); } catch (ArgumentNullException e) { result = EmailValidationResult.AddressIsEmpty; return(true); } catch (ArgumentException e) { result = EmailValidationResult.AddressIsEmpty; return(true); } catch (FormatException e) { result = EmailValidationResult.InvalidFormat; return(true); } if (mailAddress.Address != email) { result = EmailValidationResult.InvalidFormat; return(true); } ////////////////// LookupClient dnsClient = new LookupClient() { UseTcpOnly = true }; var mxRecords = dnsClient.Query(mailAddress.Host, QueryType.MX).AllRecords.MxRecords().ToList(); if (mxRecords.Count == 0) { result = EmailValidationResult.NoMailForDomain; return(true); } foreach (MxRecord mxRecord in mxRecords) { try { SmtpClient smtpClient = new SmtpClient(mxRecord.Exchange.Value); SmtpStatusCode resultCode; if (smtpClient.CheckMailboxExists(email, out resultCode)) { switch (resultCode) { case SmtpStatusCode.Ok: result = EmailValidationResult.OK; return(true); case SmtpStatusCode.ExceededStorageAllocation: result = EmailValidationResult.MailboxStorageExceeded; return(true); case SmtpStatusCode.MailboxUnavailable: result = EmailValidationResult.MailboxUnavailable; return(true); } } } catch (SmtpClientException) { } catch (ArgumentNullException) { } } if (mxRecords.Count > 0) { result = EmailValidationResult.MailServerUnavailable; return(false); } result = EmailValidationResult.Undefined; return(false); }