How to Validate or Verify Email in C# .NET?

We can validate emails with various formats using a MailAddress class file. The MailAddress class is used by the SmtpClient and MailMessage classes to store address information for email messages.

The below written in Console Application will be helpful in any dot.net application by extending its functionality in separate method.

static void Main(string[] args)
        {
            string email = "info@jd-bots.com";
            try
            {
                var emailAddr = new System.Net.Mail.MailAddress(email);
                if (emailAddr.Address == email)
                {
                    Console.WriteLine("Email Verified.");
                }
                else
                {
                    Console.WriteLine("Email Not Verified.");
                }
            }
            catch
            {
                Console.WriteLine("Email Not Verified.");
            }
        }

Positive Test Case [email = “info@jd-bots.com”]

Negative Test Case [email = “jd”]

Thank you All!!! Hope you find this useful.


Leave a Reply

Up ↑

%d