How to Validate an Email Address in Java
Join the DZone community and get the full member experience.Join For Free
Email is one of the most universal tools for sharing and receiving information across the globe, with users able to connect with others online with almost no compatibility or access issues. Using this tool, information can be instantly and securely sent to partners on the other side of the world, and personal information can be verified without divulging sensitive data about a user.
Along with this widespread use, however, comes key security measures that must take place in order to ensure the safety of your organization and data. This is particularly the case when receiving email information from previously unknown sources. These risks can include phishing attempts, malware, and other threats that can cause a negative impact to your business. Furthermore, when receiving an email address via account forms and user sign up information, you need to check that the information you are given is not only correct and real, but also that it does not lead to any malicious sources that could harm your organizational security.
The following APIs will allow you to instantly verify and validate an input email address without sending any kind of notification to the email user. This will help protect your organization in the event of any threats. The goal of this tutorial is to provide you with the tools to protect your organization’s information while providing a way to verify new accounts and user information.
This will be done through three separate functions. The first will analyze the validity of an email address’ syntax. The second will check for the address’ servers, and the third performs a full email address validation including returning the results for the previous two functions.
For all of the functions, you will first need to install the SDK library using Maven or Gradle. To install with Maven, you’ll need to add a Jitpack reference to the repository in pom.xml:
Then, you can add a reference to the dependency:
To install with Gradle, you can add the reference in your root build.gradle at the end of repositories:
Then, you can add the dependency in build.gradle:
The first function will validate whether a given email address is syntactically correct via a limited local-only check. Once you have completed the install as shown above, you can call the function:
This will return the email address’ domain, as well as confirmation on whether it is a free email provider and if it is disposable. To ensure that this API works properly, you need to ensure certain requirements are met:
- The email address is input correctly.
- You have input your API Key. This can be retrieved at no cost on the Cloudmersive website, providing 800 monthly calls across our API library.
If you need to check whether an email address’ server is valid, you can use the next API instead. This function will validate an email address by identifying whether its parent domain has email servers defined. After installing the SDK library, you can call the function as shown below:
This will return the servers, if any, for the email address in question. Though this call is less limited than the previous, it is still not as comprehensive as our next example.
This final API will perform a full email address validation, beyond the scope of the previous two. This process will check for syntactic correctness, identify the mail server, and contact the server to validate the existence of the account without sending any emails.
Once again, install the SDK library as shown above, and then call the function:
This will return whether the address, syntax, domain, and SMTP are valid, identify the mail server and domain, and confirm whether it is a catchall domain, if from a free email provider, or is disposable.
With these functions, you can tailor your validation capabilities according to your needs. If you have any questions about using these APIs or inquiries concerning other API solutions, you can visit the Cloudmersive website where our team is happy to help with anything you might need.
Opinions expressed by DZone contributors are their own.