C# Email Validation

The System.Net.Mail Had the mail address class which can be used to validate Email Ids

public static bool IsValidEmail(string emailaddress)
{
try
{
MailAddress m = new MailAddress(emailaddress);
return true;
}
catch (FormatException)
{
return false;
}
}

The code above can be used to validate Email Ids . If the Email Id is in the wrong format , It throws the Format Exception which in turn can be used as an approach to handle the invalid Email Ids . However The use of regular Expressions is the best approach as It is highly Flexible.

Hope You Liked Reading .Happy Coding 🙂 .

Advertisements