How to Validate an Email Address for Correct Syntax in C# .NET Framework

Invalid email addresses can truly be a thorn in your side, which is why having a proper validation method is crucial to filtering out this annoyance before it can cause you problems. I’m going to show you an incredibly easy implementation of this using an API as a shortcut.

Begin by opening the console of package manager and running this command to install our client.

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

Now call this function:

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 EmailPostExample{public void main(){// Configure API key authorization: ApikeyConfiguration.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 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 onlyAddressVerifySyntaxOnlyResponse result = apiInstance.EmailPost(value);Debug.WriteLine(result);}catch (Exception e){Debug.Print("Exception when calling EmailApi.EmailPost: " + e.Message );}}}}

Now any email address that you input will be checked by the API for its syntax. Super easy. This same API offers a host of other validation related functions, including name and URL validation, to name a few.

Image for post

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