How to Easily Validate Emails in .NET Core using Free APIs

Cloudmersive
3 min readMay 31, 2024

--

Email validation checks are key when we’re capturing contact information from client-side form inputs.

Without proper email validation checks in place, we run the risk of losing precious data, and we might not get the opportunity to acquire that data again.

Thankfully, using a couple of free APIs, we can handle email validation in our C#/.NET Core applications with relative ease.

Specifically, we can leverage any one of three unique validation checks — each slightly more rigorous in its assessment of email inputs than the last. The more rigorous validation services present a significant value-add for any form input handler, combining multiple complex checks into one quick API request.

We can either:

  1. Validate emails for syntactic correctness (i.e., ensuring there’s no @ missing)
  2. Validate the parent domain has email servers defined
  3. Validate syntactic correctness, check if email servers are defined, AND validate the existence of the actual account (without sending any emails)

Conveniently, we can install the SDK for all three unique APIs using a single NuGet command, and we can authorize each API call with the same free Cloudmersive API key (this will allow us to make a limit of 800 API calls per month with zero commitments).

To get started, let’s run the following command in our Package Manager console to install the SDK via NuGet:

Install-Package Cloudmersive.APIClient.NETCore.Validate -Version 2.1.6

Next, let’s copy our namespace imports from the below:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.Validate.Api;
using Cloudmersive.APIClient.NETCore.Validate.Client;
using Cloudmersive.APIClient.NETCore.Validate.Model;

Now we can use the three remaining sets of code examples to call our various email validation functions. We can handle authorization for each by replacing the “YOUR_API_KEY” placeholder text with our own API key.

Each function captures the email address as a text string input in var value or var email (remember to enclose in double quotes — e.g., “johndoe@johndoe.com”).

To call our syntactic correctness validation API, we can use the following code:

namespace Example
{
public class EmailPostExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new EmailApi();
var value = value_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
{
// Validate email adddress for syntactic correctness only
AddressVerifySyntaxOnlyResponse result = apiInstance.EmailPost(value);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EmailApi.EmailPost: " + e.Message );
}
}
}
}

And to call our email server validation API, we can use the following code:

namespace Example
{
public class EmailAddressGetServersExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

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
{
// Partially check whether an email address is valid
AddressGetServersResponse result = apiInstance.EmailAddressGetServers(email);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling EmailApi.EmailAddressGetServers: " + e.Message );
}
}
}
}

Finally, to call our comprehensive syntax, email server, AND account existence validation check (again — this does NOT involve sending any emails), we can use the following code:

namespace Example
{
public class EmailFullValidationExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

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 );
}
}
}
}

For additional context, we can review the following JSON response model for our comprehensive email validation check:

{
"ValidAddress": true,
"MailServerUsedForValidation": "string",
"Valid_Syntax": true,
"Valid_Domain": true,
"Valid_SMTP": true,
"IsCatchallDomain": true,
"Domain": "string",
"IsFreeEmailProvider": true,
"IsDisposable": true
}

That’s all there is to it!

With just a few lines of code, we can now easily take advantage of a few powerful email validation services in our C#/.NET Core applications.

--

--

Cloudmersive

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