Java - validate email address with Apache Commons

0 points
Created by:

In this article, we would like to show how to validate e-mail addresses in Java.

The important thing is: there is more than one way to write expressions that validates e-mail. Some solutions are more precise in checking correctness, others less.

Internet is filled with different solutions, so the article's main goal is to gather all of them in one place.

1. Apache Commons Validator 1.7

import org.apache.commons.validator.routines.EmailValidator;

public class EmailCheckExample {

    static boolean isValidEmail(String emailString) {
        return EmailValidator.getInstance().isValid( emailString );

    public static void main(String[] args) {
        String correctEmailToCheck = "";
        String wrongEmailToCheck = "1124Sample@ text...";
        String wrongEmailToCheck2 = "";

        System.out.println( isValidEmail( correctEmailToCheck )); // true
        System.out.println( isValidEmail( wrongEmailToCheck ));   // false
        System.out.println( isValidEmail( wrongEmailToCheck2 ));  // false



2. Regular expressions

We have already written about checking email addresses using regular expressions here - Java - validate email with regex.

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂