How to validate email addresses in Salesforce Apex
Email validation is a great tool, but all too often it functions only syntactically by simply checking the format of the address in question. A more robust system with automated server querying is quite a bit more difficult to implement, however. That’s why today we are demonstrating how to use one of our APIs to get the ideal results in a fraction of the time.
We begin by downloading the Cloudmersive API client for validation and then copying the /client folder from our archive into the project’s folder.
With our client in place, we are now in a position to call emailFullValidation:
SwagEmailApi api = new SwagEmailApi();SwagClient client = api.getClient();// Configure API key authorization: ApikeyApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{'email' => 'email_example'};try {// cross your fingersSwagFullEmailValidationResponse result = api.emailFullValidation(params);System.debug(result);} catch (Swagger.ApiException e) {// ...handle your exceptions}
Here is an example result, so you can see the information provided.
{
"ValidAddress": true,
"MailServerUsedForValidation": "string",
"Valid_Syntax": true,
"Valid_Domain": true,
"Valid_SMTP": true,
"IsCatchallDomain": true,
"Domain": "string",
"IsFreeEmailProvider": true,
"IsDisposable": true
}
Done!