Validating EmailAddress in Java

I explored the best way to check if String valid email address. I am now tied to two parameters, namely: javax.mail.internet.InternetAddress ; or using the Apache Commons EmailValidator , which internally uses a sophisticated regular expression parser.

I was wondering if there are any advantages to choosing one over the other in terms of correctness, or is it just fine? I know that InternetAddress does not handle non-ascii characters efficiently in some cases.

+4
source share
3 answers

You can use EmailValidator from the Validator Apache Commons library to do this:

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

The isValid method checks if the field has a valid email address.

This is the best way to verify your Java email address according to this question. What is the best method to verify your Java email address?

+12
source

For what was set as the format of the email address, the difference between the two approaches is negligible. And again, fifty years ago, people never saw the need to use 4 digits to encode years, so ...

The only “mistake” using the regular expression from Apache Commons is that its functionality for checking email addresses is not a “Java standard”. To what extent does this affect you as a developer? depends on how paranoid you are.

On the other hand, a standard Java implementation may be less efficient. You will need to build InternetAddress and check it out. If you look at the JavaMail 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) */ 

( XXX seems to be a bit like a note or “do” item)

+1
source

I just tested it and it seems that the performance in InternetAddress is better than 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 = " test@testy.com "; 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

Internet Destination Duration: 67

The results are such that EmailValidator takes ~ 20 times longer:

0
source

All Articles