Home > front end >  ASP.NET Core Email Validation with API
ASP.NET Core Email Validation with API

Time:09-02

I am trying to validate an email whether it is real or fake. I am using some code but it always returns Ok.

This is my code:

public string validateEmail(string userEmail)
{
        try
        {
            string key = "xxxx";
            string email = "xxxx";
            var crmUrl = "https://app.emaillistvalidation.com/api/verifEmail?secret="   key   "&email="   email;
            string url = crmUrl;

            var client = new RestClient(url);
            client.RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });

            var request = new RestRequest();
            request.AddHeader("authorization", "Basic xxxxxxxxxxxxxxxxxx");

            IRestResponse response = client.Execute(request);

            var content = response.Content;
            return content;
        }
        catch (Exception ex)
        {
        }

        return default;
}

What is wrong here? And what can I do instead?

CodePudding user response:

I am trying to validate an email whether it is real or fake. I am using some code but it always returns Ok.

Well I have gone through the code and the API You have shared. I also register and open a demo api on https://app.emaillistvalidation.com/api and its behaving as expected for both valid and invalid email. Here are my steps:

Register on app.emaillistvalidation.com

enter image description here

My Controller Code

        [Route("ValidateEmail")]
        [HttpGet]
        public async Task<IActionResult> ValidateEmail(string emailId)
        {
            var handler = new HttpClientHandler();
            var data = "";

            handler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };
            var client = new HttpClient(handler);
            client.DefaultRequestHeaders.Accept.Clear();

            var responseFromApi = await client.GetAsync("https://app.emaillistvalidation.com/api/verifEmail?secret="   apiSecret   "&email="   emailId   "");
            if (responseFromApi.IsSuccessStatusCode)
            {
                data = await responseFromApi.Content.ReadAsStringAsync();
               
            }

            return Ok(data);
        }

Output For Valid & Invalid Email

enter image description here

Postman Direct Test

I have also tested on postman for valid email I got ok response and for invalid and wrong email I got unknown and disable response.:

Valid Email: enter image description here

Wrong Email:

enter image description here

Wrong Domain:

enter image description here

Note: Please double check the way you are trying. You could have a look on my code and the way I have tested.

  • Related