
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to validate an email address in C#?
There are several ways to validate an email address in C#.
System.Net.Mail −The System.Net.Mail namespace contains classes used to send electronic mail to a Simple Mail Transfer Protocol (SMTP) server for delivery.
System.Text.RegularExpressions − Represents an immutable regular expression.
Use the below expression
@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([azA-Z]{2,4}|[0-9]{1,3})(\]?)$"
We can use the MailAddress class of the System.Net.Mail namespace to validate an email address
Example
using System; using System.Net.Mail; namespace DemoApplication{ class Program{ public static void Main(){ try{ string email = "hello@xyzcom"; Console.WriteLine($"The email is {email}"); var mail = new MailAddress(email); bool isValidEmail = mail.Host.Contains("."); if(!isValidEmail){ Console.WriteLine($"The email is invalid"); } else { Console.WriteLine($"The email is valid"); } Console.ReadLine(); } catch(Exception){ Console.WriteLine($"The email is invalid"); Console.ReadLine(); } } } }
Output
The output of the above code is
The email is hello@xyzcom The email is invalid
Example using Regex −
We can also use regular expression to validate an email address.
Example
using System; using System.Text.RegularExpressions; namespace DemoApplication{ public class Program{ public static void Main(){ string email = "hello@xyz.com"; 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); Console.WriteLine($"The email is {email}"); bool isValidEmail = regex.IsMatch(email); if (!isValidEmail){ Console.WriteLine($"The email is invalid"); } else { Console.WriteLine($"The email is valid"); } Console.ReadLine(); } } }
Output
The output of the above code is
The email is hello@xyz.com The email is valid
- Related Articles
- How to validate an email address in PHP ?\n
- How to validate email address in JavaScript?
- Validate Email address in Java
- How to validate an email address using Java regular expressions.
- Python program to validate email address
- How to validate email address using RegExp in JavaScript?
- How to validate Email Address in Android on EditText using Kotlin?
- How to validate email using jQuery?
- C Program to validate an IP address
- How to validate an email id using regular expression in Python?
- How to validate URL address in JavaScript?
- How should I validate an e-mail address in Android?
- How to validate the IP address using PowerShell?
- Validate IP Address in C#
- Validate IP Address in Python

Advertisements