|
The EmailValidator component verifies if an email address is valid, properly formatted and really exists.
This component uses different levels of email tests: it can check the email syntax, test the domain availability, check whether the SMTP service is available and, finally, check if the mailbox exists.
The Email validation component can be used in C#, VB.NET, Delphi, C++Builder, Javascript and VBScript: Download
Join our Mailing List and stay tuned: SUBSCRIBE
|
The Clever Internet Suite library includes a special component, EmailValidator, by using of which you can check the email syntax, mail server existence and it's availability. In some cases, you can check whether the specific user has the mailbox on this server and whether this mailbox can accept mail. You can set up the desired validation tests by using of the ValidationLevel component property.
All validation tests are sorted by level:
- Check whether the Email is blacklisted. The ValidationLevel is set to EmailValidationLevel.Blacklist in C# (vlBlacklist in Delphi). The blacklisted Emails should be added to the BlackList property;
- Check the Email address syntax. See the EmailValidationLevel.Syntax (vlSyntax) option. The Regex pattern for validation is stored within the SyntaxPattern property;
- Check the MX information for the Email address. See the EmailValidationLevel.Domain (vlDomain) option. Tf this level is selected, the component queries the MX record from DNS on the Internet;
- Check whether the SMTP service is available on the Network. See the EmailValidationLevel.Smtp (vlSmtp) option. The component resolves the MX server address and attempts to connect to it. If the connection fails, the validation fails as well;
- Check whether the mailbox is available. See the EmailValidationLevel.Mailbox (vlMailbox) option. The component connects to MX server and sends the "RCPT TO" command. If server rejects this command, the validation fails.
Please note! When you set the validation level, the component always performs all previous validation levels as well. E.g., if you set the EmailValidationLevel.Domain level, the component performs 1, 2 and 3 steps above; if you set the EmailValidationLevel.Smtp level, the steps 1, 2, 3 and 4 are performed, etc.
Since the component performs all previous validation tests, the email may fail any of them. You receive the corresponding validation result.
E.g., if you set the ValidationLevel to EmailValidationLevel.Smtp and the Validate function returns EmailValidationResult.DomainOk, this means that the email passed the validation steps 1, 2 and 3. But 4 was failed.
So the result may be interpreted as follows: the tested email is not blacklisted, it has valid syntax and the email domain has valid MX record. But the responsible SMTP service is not available on the Network. This may be temporary error or some IP access problems. If you run the EmailValidator on the PC from which you plan to send messages to this email address, this result indicates that your message will not be delivered. Note: this is true for relaying the message directly to recipient's mailbox in the save way as an email server does.
In case if you set the validation level to EmailValidationLevel.Mailbox, depending on email address, you may get any of validation results. Ideally, you get the EmailValidationResult.MailboxOk (vrMailboxOk in Delphi) result.
Please note, if you run the EmailValidator on your working PC, the Mailbox validation may fail because email servers block connections from unknown hosts (see the note above). So the best way to pass this validation test is to run the EmailValidator on the same PC as your own email server runs (from which you plan to send emails). To successfully pass the Mailbox validation, you need to set up both the EmailFrom and HostName properties. The EmailFrom specifies the sender email address that will be used in the "MAIL FROM" command while MailBox validation. The HostName is used when connecting to the SMTP service.
// [Delphi] validator.BlackList.Add('blacklisted@domain.com'); validator.EmailFrom := 'your_email_to_sent_test_messages_from'; validator.ValidationLevel := vlMailbox;
//returns vrInvalid, since the address is blacklisted result := validator.Validate('blacklisted@domain.com');
//returns vrBlacklistOk, because it is not blacklisted, but the syntax is invalid result := validator.Validate('foo.bar');
//returns vrSyntaxOk, because the domain name is not registered result := validator.Validate('foo.bar@bad-domain-name.com');
//gets vrSmtpOk in case if valid-registered-domain.com has valid MX record, //but may return vrMailboxOk in case if the mail server doesn't //provide an information about invalid users result := validator.Validate('bad-user@valid-registered-domain.com');
//should return vrMailboxOk result := validator.Validate('valid-user@valid-registered-domain.com'); |
The same code for C#:
// [C#] validator.BlackList = new string[] { "blacklisted@domain.com" }; validator.EmailFrom = "your_email_to_sent_test_messages_from"; validator.ValidationLevel = EmailValidationLevel.Mailbox;
EmailValidationResult result = validator.Validate("blacklisted@domain.com");
result = validator.Validate("foo.bar");
result = validator.Validate("foo.bar@bad-domain-name.com");
result = validator.Validate("bad-user@valid-registered-domain.com");
result = validator.Validate("valid-user@valid-registered-domain.com"); |
The usage of EmailValidator component is really simple:
// [Delphi] procedure TForm1.Button1Click(Sender: TObject); const results: array[TclEmailValidationResult] of string = ('Invalid address', 'Blacklist Ok', 'Syntax Ok', 'Domain Ok', 'Smtp Ok', 'Mailbox Ok'); var res: string; begin clEmailValidator1.DnsServer := 'dns_server_ip'; clEmailValidator1.EmailFrom := 'your_email_to_sent_test_messages_from'; clEmailValidator1.ValidationLevel := vlMailbox; res := results[clEmailValidator1.Validate('email_to_validate')]; ShowMessage(res); end; |
// [C#] private void button1_Click(object sender, EventArgs e) { using (EmailValidator validator = new EmailValidator()) { validator.BlackList = new string[] { "blackl@domain.com" }; validator.DnsServer = "dns_server_ip"; validator.EmailFrom = "your_email_to_sent_test_messages_from"; validator.ValidationLevel = EmailValidationLevel.Mailbox; EmailValidationResult result = validator.Validate("email_to_validate"); } } |
Learn more about Clever Internet Suite
Download Clever Internet Suite for Delphi and Clever Internet Suite for C# .NET
Sergey Shirokov Clever Components team
www.clevercomponents.com
|