Check if an Email Address is correct and really exists using Java.
You can easily test if an Email Address is formatted correctly with a regex, like in the example bellow. But it will miss many important cases, like if the Email Address is well formed but the emails will bounce, because the account doesn’t exist or has recently been disabled.
if ("foo@bar.com".matches("[^@]+@[^@]+\\.[^@]+")) {
// email matches pattern
} else {
// email doesnt match pattern
}
Email Validation API
To do full email validation inspecting the mail server you can use MailValidation.io and make a requests call to check it. This will do the check via the real email service which will make sure the requests are not blocked.
String email = ???;
String apiKey = ???;
HttpClient client = HttpClient.newBuilder().build();
String jsonResult = client.send(
HttpRequest.newBuilder()
.POST(HttpRequest.BodyPublishers.ofString("{\"email\":" + email + "}"))
.uri(URI.create("https://app.mailvalidation.io/a/{team_slug}/validate/api/validate/"))
.setHeader("Authorization", "Api-Key " + apiKey)
.body(
.build(),
HttpResponse.BodyHandlers.ofString(),
).body()
String status = new JSONObject(jsonResult).getString("is_valid");
if (status == "true") {
// the email is valid
} else {
// the email is invalid
}
Be sure to signup and get your own API key.
Get started with the best Email Validator today!