How to Validate an Email Address in Salesforce Apex
Whether you are sending or receiving emails from a user who input their address on your website/application, it is important to take proper security precautions ensuring they are both safe to interact with and syntactically correct. The following API will automatically perform a full validation of an input email address by checking the syntax, identifying the mail server, and contacting the email server to validate the existence of the account — all without sending any emails.
To use this validation API in Salesforce Apex, we first need to download and copy the /client folder into the Apex project:
Following this step, we are going to configure the API key and call the validation function:
SwagEmailApi api = new SwagEmailApi();
SwagClient client = api.getClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) client.getAuthentication('Apikey');
Apikey.setApiKey('YOUR API KEY');Map<String, Object> params = new Map<String, Object>{
'email' => 'email_example'
};try {
// cross your fingers
SwagFullEmailValidationResponse result = api.emailFullValidation(params);
System.debug(result);
} catch (Swagger.ApiException e) {
// ...handle your exceptions
}
That’s it! The response will include all the information you need to validate the target email address.