Email validation is crucial to help you maintain a list of valid and real email addresses. Keeping mistyped and non-existent emails will result in high bounces, low opens, and few conversions. Worse, it can also damage your sending reputation, which can be difficult to repair.Â
Hence, adding email validation in your C# code is essential. And fortunately, relatively painless! In this post, youâll learn several options on how to validate email in C#.Â
Letâs start!
Validate email addresses using the MailAddress class
Microsoft provides built-in support for sending emails to an SMTP server through the System.Net.Mail namespace. It contains the MailAddress class, which is used to store the sender and recipient email addresses. You can utilize this class to validate emails.
Note: This article focuses on email validation. If youâre looking for how to send and receive an email in C# using the classic SMTP protocol, see Send and Receive Emails in ASP.NET C#.
To see how we can use the MailAddress class to check whether an email is valid, letâs create a new C# Console App using Visual Studio. Either the .NET Framework or Core will work. Then, copy-paste the following code into the Program.cs file.