;

How to Validate an Email Address In C#


Tutorialsrack 10/02/2021 C#

In this article, you will learn how to validate an email address in c#. There are various ways to validate an email address in C#. In this article, we validate the Email address using MailAddress Class and Using Regular Expression.

Here are the examples to validate an email in C#

Example 1: Validate Email Address using MailAddress Class of the System.Net.Mail namespace

In this example, we used the MailAddress class of the System.Net.Mail namespace to validate an email address. The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.


Here is the source code of the program to Validate an Email using MailAddress Class in C#.

Example 1: Validate Email Address using MailAddress Class of the System.Net.Mail namespace
using System;
using System.Net.Mail;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Validate an Email in C# */
        static void Main(string[] args)
        {

            Console.WriteLine(IsValidEmail("xyz@xyz.com")==true?"Valid":"Invalid");

            Console.WriteLine(IsValidEmail("xyz@xyzcom") == true ? "Valid" : "Invalid");
            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static bool IsValidEmail(string email)
        {
            try
            {
                var mail = new MailAddress(email);
                bool isValidEmail = mail.Host.Contains(".");
                if (!isValidEmail)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
Output
Valid
Invalid

Example 2: Validate an Email Address Using Regular Expression

In this example, we used a regular expression to validate an email address. We used  the IsMatch() method of Regex class which belongs to  System.Text.RegularExpressions namespace.

Here is the source code of the program to Validate an Email using IsMatch() method of Regex Class in C#.

Example 2: Validate an Email Address Using Regular Expression
using System;
using System.Text.RegularExpressions;

namespace Tutorialsrack
{
    class Program
    {
        /* How to Validate an Email in C# */
        static void Main(string[] args)
        {

            Console.WriteLine(IsValidEmail("xyz@xyz.com")==true?"Valid":"Invalid");

            Console.WriteLine(IsValidEmail("xyz@xyzcom") == true ? "Valid" : "Invalid");
            //Hit ENTER to exit the program
            Console.ReadKey();
        }

        public static bool IsValidEmail(string email)
        {
            try
            {
                Regex regex = new Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.
                 [0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$",
                RegexOptions.CultureInvariant | RegexOptions.Singleline);
                bool isValidEmail = regex.IsMatch(email);
                if (!isValidEmail)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
Output
Valid
Invalid

I hope this article will help you to understand how to validate an email address in c#.

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags