.NET Framework Developer Center >
.NET Development Forums
>
Network Class Library (System.Net)
>
validate an email address?
validate an email address?
- how can I validate if the email address provided by the user does exists or not before proceeding to send an email using SmtpClient in C#?
Jassim Rahma
Answers
- Marked As Answer byJassim Rahma Tuesday, September 08, 2009 2:06 PM
All Replies
- Marked As Answer byJassim Rahma Tuesday, September 08, 2009 2:06 PM
- Code snippet if the email address is like xxx_xxxx@xx.com:
string pattern = @"[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]@[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]-[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|].com"; System.Text.RegularExpressions.Match match = System.Text.RegularExpressions.Regex.Match(editemail.Text.Trim(), pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); if (!match.Success) { MessageBox.Show("You must specify valid E-mail", this.vGlobalVariables.gProductVersion, MessageBoxButtons.OK, MessageBoxIcon.Warning); this.editemail.Focus();}
General regex pattern related to your requirement can be found in http://www.regular-expressions.info/email.html
Cheers:)
Mark as answer if it helps to solve your query - Hello Jassim,
in my company we use EmailVerify.NET (http://www.emailverify.net), a Microsoft .NET component which supports e-mail syntax checking, domain MX test and mailbox validation too.
E-mail validation up to the mailbox level is easy as writing the following code block:
var verifier = new EmailVerifier(); var result = verifier.Verify("me@example.com", VerificationLevel.Mailbox); if (result.IsSuccess) { // Go on, send the message }
Additional code samples can be found here.
From their home page:
EmailVerify.NET is a powerful .NET component which verifies email addresses using various techniques, including:- Syntax verification, according to RFC 2821 and RFC 2822
- MX record lookup
- Disposable email address (DEA) validation
- SMTP availability check
- Mailbox existence check, with greylisting support
- Catch-all test
It is completely written in managed code (C#) and is compliant with the Common Language Specification (CLS), so it can be used with any other .NET language, including VB.NET, C++/CLI, J#, IronPython, IronRuby and F#.
The price for this component is very small (less than 50 bucks) and their support is one of the greatest I've ever found for a small software company like they are.
Hope this helps.


