How Abstract API Handles Email Validation in Spring Boot
Abstract API addresses the core weaknesses of traditional validation methods through a multi-layered verification process that goes far beyond simple pattern matches.
- The service combines multiple checks into one API call. These include syntax validation, autocorrection, MX and SMTP lookups, and checks for disposable, role-based, or free domains.
- A clear deliverability field allows you to decide immediately whether to accept an email, so you avoid delays from hard bounces.
- It offloads maintenance. The Abstract team keeps its disposable domain lists and validation rules current, so your application code remains static as rules evolve.
- The service provides operational guarantees like a 99.99% SLA. You do not need to run your own probes or manage network issues because integration is a simple HTTPS call.
- It uses a stable, versioned JSON structure. This structure maps easily to a record or DTO in your Spring application for type-safe data transfer.
How to Set Up Abstract API in Your Project
Once you know Abstract’s capabilities, you can add its email validation API to your project with ease.
- First, add the reactive WebClient dependency to your project. For a Gradle project, you would add
implementation("org.springframework.boot:spring-boot-starter-webflux")
. - Next, place your API key in the
application.yml
configuration file. This action separates your credentials from your code.
abstract:
email:
api-key: YOUR_KEY
Sample Email Validation Implementation with Abstract API
The implementation requires a service to call the API and a data transfer object (DTO) to hold the response. You can then inject this service into a custom Spring ConstraintValidator
to check emails during the validation lifecycle. The code below shows all the necessary components.
// 1. DTO to map the JSON response
public record AbstractEmailResponse(
String email,
String deliverability,
float quality_score,
IsValid is_valid_format,
IsFlag is_disposable_email,
IsFlag is_role_email,
IsFlag is_free_email,
IsFlag is_catchall_email,
IsFlag is_mx_found,
IsFlag is_smtp_valid,
String autocorrect
) {}
public record IsValid(boolean value, String text) {}
public record IsFlag(boolean value, String text) {}
// 2. WebClient configuration
@Configuration
public class ApiConfig {
@Bean
WebClient abstractEmailClient(WebClient.Builder builder) {
return builder.baseUrl("https://emailvalidation.abstractapi.com/v1").build();
}
}
// 3. Service to call the API
@Service
public class EmailValidationService {
private final WebClient client;
@Value("${abstract.email.api-key}")
private String apiKey;
public EmailValidationService(WebClient abstractEmailClient) {
this.client = abstractEmailClient;
}
public Mono<AbstractEmailResponse> validate(String email) {
return client.get()
.uri(uriBuilder -> uriBuilder
.queryParam("api_key", apiKey)
.queryParam("email", email).build())
.retrieve()
.bodyToMono(AbstractEmailResponse.class);
}
}
// 4. Use in a custom validator
@Component
public class EmailValidator implements ConstraintValidator<ValidEmail, String> { @Autowired
private EmailValidationService validationService;
@Override
public boolean isValid(String email, ConstraintValidatorContext context) {
if (email == null || email.isBlank()) {
return true; // Let @NotBlank handle this
}
AbstractEmailResponse response = validationService.validate(email)
.block(Duration.ofSeconds(5));
return response != null
&& "DELIVERABLE".equals(response.deliverability())
&& response.quality_score() > 0.7;
}
}
When the service calls the API, it receives a JSON response with rich details about the email address. Here is a sample response for a valid email.
{
"email":"johnsmith@gmail.com",
"autocorrect":"",
"deliverability":"DELIVERABLE",
"quality_score":0.9,
"is_valid_format":{"value":true,"text":"TRUE"},
"is_free_email":{"value":true,"text":"TRUE"},
"is_disposable_email":{"value":false,"text":"FALSE"},
"is_role_email":{"value":false,"text":"FALSE"},
"is_catchall_email":{"value":false,"text":"FALSE"},
"is_mx_found":{"value":true,"text":"TRUE"},
"is_smtp_valid":{"value":true,"text":"TRUE"}
}
This response tells you several key facts:
- The format is correct, and the API suggests no autocorrection.
- Abstract could reach the mail exchange (MX) server and confirm the SMTP status, so the mailbox almost certainly exists.
- The domain is a free provider but not a disposable one. It is a personal address, not a role-based one like support@ or admin@.
- A high
quality_score
with aDELIVERABLE
status means you can safely accept the email for user signup.
Final Thoughts
Traditional validation through regular expressions often fails. It cannot detect disposable domains, catch-all addresses, or simple typos. Abstract API offers a robust alternative with its multi-layer checks.
To reliably validate user emails, consider an account on Abstract API and get your free API key.