Built-in method to validate email address in C#

Are you looking for a built-in method in C# which lets you validate email address. If yes , you can use the EmailAddressAttribute class that is defined in the System.ComponentModel.DataAnnotations namespace to do it.

How to validate email address in C# using EmailAddressAttribute class ?

Below is a sample code snippet demonstrating the usage of the EmailAddressAttribute class to validate the email address in C#.

using System;
using System.ComponentModel.DataAnnotations;

namespace ACCode
{
    class Program
    {
        static void Main(string[] args)
        {
            EmailAddressAttribute validator = new EmailAddressAttribute();
            var result1 = validator.IsValid("test@gmail");
            var result2 = validator.IsValid("[email protected]");
            Console.WriteLine(result1);
            Console.WriteLine(result2);
            Console.ReadLine();
        }
      
    } 
}