How to Validate Email Address in C#

Check if an email address is correct and really exists using C#

Email Validation in a Nutshell

An Email Address can look right but still be wrong and bounce. We’re providing you our api to use C# and validate all those emails coming into your various workspaces.

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check at one time, or how fast you can check them. In C# the example changes to add the API key. You can of course also just use our bulk email validation upload tool if you aren’t interested in using the api and C#.

				
					using System;
using System.Net.Http;
using System.Net.Http.Headers;

class Program
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        string apiKey = ""; // Generated in your User Profile; it shows at the top in a green bar once
        string teamSlug = ""; // When you sign up, you have a team; it's in the URL; then use that
        string emailAddress = "test@test.com"; // The test email

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Add("Authorization", "Api-Key " + apiKey);

            string url = "https://app.mailvalidation.io/a/" + teamSlug + "/validate/api/validate/";
            var content = new StringContent("{\"email\": \"" + emailAddress + "\"}", System.Text.Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync(url, content);
            if (response.IsSuccessStatusCode)
            {
                dynamic result = await response.Content.ReadAsAsync<dynamic>();
                bool isValid = result["is_valid"];
                if (isValid)
                {
                    Console.WriteLine("Valid");
                }
                else
                {
                    Console.WriteLine("Invalid");
                }
            }
            else
            {
                Console.WriteLine("An error occurred: " + response.StatusCode);
            }
        }
    }
}
				
			

Support

Are you having trouble, have some questions. How can we help?