Skip to content

Instantly share code, notes, and snippets.

@woodss
Created December 14, 2014 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodss/0aa6e0e787b76e6c6a6b to your computer and use it in GitHub Desktop.
Save woodss/0aa6e0e787b76e6c6a6b to your computer and use it in GitHub Desktop.
How to validate an e-mail address in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace MailChecking
{
class Program
{
static void Main(string[] args)
{
string EmailToCheck = Console.ReadLine();
if (IsValidEmail(EmailToCheck))
{
Console.WriteLine("Yes, that was a valid e-mail address!");
}
else
{
Console.WriteLine("No, that was not a valid e-mail address!");
}
Main(args);
}
private static bool IsValidEmail(string EmailToCheck)
{
try
{
MailAddress mail = new MailAddress(EmailToCheck);
return true;
}
catch (Exception e)
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment