How to validate email addresses using C# in .NET Framework

Well executed email validation can save you a ton of headache. It lets you filter out fake user submissions and avoid wasting bandwidth when sending out emails, just to name a couple. Unfortunately, most email validation is pretty basic and only checks the email address’s format. This can easily be fooled. Today we will be looking at how to use an API to perform full validation on an email address, querying the mail server to get a guaranteed result. Let’s dive right in.

First, install the API client by running the following command in the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.Validate -Version 1.2.7

Now, simply run the EmailFullValidation function and provided it with your email address.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.Validate.Api;
using Cloudmersive.APIClient.NET.Validate.Client;
using Cloudmersive.APIClient.NET.Validate.Model;
namespace Example
{
public class EmailFullValidationExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new EmailApi();
var email = email_example; // string | Email address to validate, e.g. \"support@cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try
{
// Fully validate an email address
FullEmailValidationResponse result = apiInstance.EmailFullValidation(email);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EmailApi.EmailFullValidation: " + e.Message );
}
}
}
}

And we are already done. Our response will include a variety of useful data, including the syntax and domain validity, as well as the domain name and whether it is a free email provider. Here is an example using support@cloudmersive.com:

{
"ValidAddress": true,
"MailServerUsedForValidation": "mx.zoho.com",
"Valid_Syntax": true,
"Valid_Domain": true,
"Valid_SMTP": true,
"IsCatchallDomain": false,
"Domain": "cloudmersive.com",
"IsFreeEmailProvider": false,
"IsDisposable": false
}

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store