EmailAddress Validation in Java

24,409

Solution 1

You can use an EmailValidator from Apache Commons Validator library for that:

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

EmailValidator validator = EmailValidator.getInstance();
if (validator.isValid(email)) {
   // is valid, do something
} else {
   // is invalid, do something
}

isValid method checks if a field has a valid e-mail address.

This is the best Java email address validation method according to this question What is the best Java email address validation method?

Solution 2

For something as well-established as email address format, the difference between two approaches is minuscule. Then again, fifty years ago, people never saw the need to use 4 digits for encoding years, so...

The only 'pitfall' with using the regex from Apache Commons, is that its functionality for validating an email address isn't "Java standard". To what extent that affects you as a developer? depends on how paranoid you are.

On the other hand, the standard Java implementation might be less efficient. You'd have to construct an InternetAddress and validate it. Looking at JavaMail's source code, I could see this:

/**
 * Check that the address is a valid "mailbox" per RFC822.
 * (We also allow simple names.)
 *
 * XXX - much more to check
 * XXX - doesn't handle domain-literals properly (but no one uses them)
 */

(The XXX seems to be some sort of a note, or a "to do" item)

Solution 3

I've just tested it, and apparently the performance on InternetAddress is substantially better then using EmailValidator

package com.avaya.oss.server.errors;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import org.apache.commons.validator.EmailValidator;

public class TestValidationTypes {

    static String email = "[email protected]";
    static int maxItr = 10000;

    public static void main(String[] args) throws AddressException {

        long start = System.currentTimeMillis();
        for (int i = 0; i < maxItr; i++) {
            EmailValidator.getInstance().isValid(email);
        }
        System.out.println("EmailValidator duration: " + (System.currentTimeMillis() - start));

        start = System.currentTimeMillis();
        for (int i = 0; i < maxItr; i++) {
            InternetAddress internetAddress = new InternetAddress(email);
            internetAddress.validate();
        }
        System.out.println("InternetAdress duration: " + (System.currentTimeMillis() - start));

    }

}

Output:

EmailValidator duration: 1195

InternetAdress duration: 67

The results are that EmailValidator took ~20 times longer:

Share:
24,409
kuriouscoder
Author by

kuriouscoder

Updated on September 09, 2020

Comments

  • kuriouscoder
    kuriouscoder over 3 years

    I was researching best possible way to check if a String was a valid email Address. I am now fixated on two options, viz., using javax.mail.internet.InternetAddress; or using Apache Commons EmailValidator, which internally uses complicated regex parser.

    I was wondering if there is any advantages on picking one over the other in terms of correctness, or is both just fine? I know for a fact that InternetAddress doesn't handle non-ascii characters efficiently in some cases.

  • Saravanabalagi Ramachandran
    Saravanabalagi Ramachandran over 8 years
    user@localhost is considered valid. bla@bla is considered valid. someone@[10.10.1.5] is also considered valid. I think you would want to treat them invalid. So I think spend 20 times longer is worth it.
  • Bilbo Baggins
    Bilbo Baggins almost 7 years
    this validator allows this email: ##$$$$#$%&&***[email protected] with this code. EmailValidator.getInstance(false).isValid(email);
  • Bilbo Baggins
    Bilbo Baggins almost 7 years
    My bad, As per this it is valid email, en.wikipedia.org/wiki/Email_address#Local-part
  • Rohan
    Rohan almost 3 years
    For anyone stumbling on this years later, JMail is faster and more correct than both of these options. Plus, it is customizable so you can treat addresses with domain literals (like user@localhost) as invalid.
  • Mohammad Irfan
    Mohammad Irfan about 2 years
    is the validation works on multiple recipient seperated by comma or collon?