How to Validate an Email Address using Java

Cloudmersive
2 min readMay 11, 2022

If your app is storing contact info, it’s important to make sure that info is entered correctly, or you might end up with a messy problem to solve later. With the help of our Email Validation API, your app can out if an address is syntactically correct, identify the relevant mail server (if it exists), and contact the email server without sending any actual emails. You can include this API in your project easily by following instructions included below for Java.

First install the Java SDK with Maven. To begin, add a reference to the repository in pom.xml:

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

Next up, add one to the dependency:

<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>

Finally, include the import classes and then copy the below code examples to call the validation API:

// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.EmailApi;
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");
EmailApi apiInstance = new EmailApi();
String email = "email_example"; // String | Email address to validate, e.g. \"support@cloudmersive.com\". The input is a string so be sure to enclose it in double-quotes.
try {
FullEmailValidationResponse result = apiInstance.emailFullValidation(email);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling EmailApi#emailFullValidation");
e.printStackTrace();
}

Just like that, you’re all done — easy as pie. If this is your first time using a Cloudmersive API, don’t forget to head to our website and register for a free account to receive a Cloudmersive API key.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.