Description
In this article, I am going to write C# code examples to Check or Validate Email Address/Email ID using Regular Expressions and C# code example to Test EMail Address is valid or not by sending Test mail.
Summary
- Validate Email Address in C# using Regular Expressions
- Validate Email Address in C# using Regular Expressions (2)
- Validate Email Address in C# using Regular Expressions (3)
- Validate Email Address Pattern and Test Email ID is Valid or Not in C#
- Validate Email Address Pattern and Check Is Valid E Mail Domain
Validate Email Address in C# using Regular Expressions
Here, we are using Regex class to represents Regular Expressions
public static bool IsValidEmailAddressByRegex(string mailAddress) { Regex mailIDPattern = new Regex(@"[w-]+@([w-]+.)+[w-]+"); if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress)) { return true; } else { return false; } }
Validate Email Address/Email ID in C# using Regular Expressions(2)
public static bool IsValidEmailAddressByRegex2(string mailAddress) { Regex mailIDPattern = new Regex(@"^(?!.)(""([^""r]|[""r])*""|" + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!.).)*)(?<!.)" + @"@[a-z0-9][w.-]*[a-z0-9].[a-z][a-z.]*[a-z]$", RegexOptions.IgnoreCase); if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress)) { return true; } else { return false; } }
Validate Email Address in C# using Regular Expressions(3)
public static bool IsValidEmailAddressByRegex3(string mailAddress) { Regex mailIDPattern = new Regex("^((([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+(.([a-z]|d|[!#$%&'*+-/=?^_`{|}~]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])+)*)|((x22)((((x20|x09)*(x0dx0a))?(x20|x09)+)?(([x01-x08x0bx0cx0e-x1fx7f]|x21|[x23-x5b]|[x5d-x7e]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(\([x01-x09x0bx0cx0d-x7f]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))))*(((x20|x09)*(x0dx0a))?(x20|x09)+)?(x22)))@((([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|d|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).)+(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])|(([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])([a-z]|d|-|.|_|~|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF])*([a-z]|[u00A0-uD7FFuF900-uFDCFuFDF0-uFFEF]))).?$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled); if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress)) { return true; } else { return false; } }
Validate Email Address Pattern and Test Email ID is Valid or Not in C#
You can use this C# function to validate E Mail Address string pattern and to Test the given E Mail ID is Valid Address or not by sending the Test mail.
public static bool ValidateAndTestEmailAddress(string mailAddress) { Regex mailIDPattern = new Regex(@"[w-]+@([w-]+.)+[w-]+"); if (!string.IsNullOrEmpty(mailAddress) && mailIDPattern.IsMatch(mailAddress)) { System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(); // your mail server address smtpClient.Host = "smtp.server1.net"; // your mail server port address smtpClient.Port = 25; // Credentials to use mail server if required smtpClient.Credentials = new NetworkCredential("[email protected]", "yourpassword"); try { // Send Test Mail to Check Email Address/E mail ID is Valid or Not ... smtpClient.Send("[email protected]", mailAddress, "Mail ID testing", "Validate Mail Address"); // Email Address was Verified! return true; } catch (Exception exp) { //Failed to send test mail... Console.WriteLine(exp.Message); return false; } } else { //Invalid email address pattern... return false; } }
Validate Email Address Pattern and Check Is Valid EMail Domain
You can use this Regex pattern to validate Email Address string pattern and also to check whether the given EMail Address contains Valid Email Domain or Not.
From http://www.regular-expressions.info/email.html
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@ (?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil| biz|info|mobi|name|aero|asia|jobs|museum)b
Thanks,
Morgan
Software Developer
Advertisement
string uname = Convert.ToString(dto.Rows[0]["USER_NAME"]);
String expression = @"w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*";
Regex regex = new Regex(expression);
Match m = regex.Match(uname);
if (m.Success)
{
emailId = uname;
}
It's enormous that you are getting thoughts from this
post as well as from our discussion made at this time.
Excellent web site you have here.. It's hard to find excellent writing
like yours nowadays. I honestly appreciate people like you!
Take care!!