Java implementation of e-mail address validation according strictly to RFC 822.
Proper validation of e-mail address format is a surprisingly complicated task: For this seemingly simple format, there are, in fact, many formats in existence, there are many specifications for those formats and there are many validators differing in what format exactly are they expecting and validating.
I needed a validator which would strictly validate exactly the format defined in the original RFC 822 (specifically, its addr-spec
production). So this library does exactly that. It is implemented directly according to the syntax specification in the RFC so anyone can check if it is correct without need to read super-complicated regexes.
Beware! This is usually not the best format and validator to use for e.g. user-facing e-mail address inputs, etc. The RFC 822 format allows many things considered unacceptable for general use, e.g. domain literals (joe@[192.168.0.2]
), strange and escaped characters (very.unusual."@"[email protected]
), etc. You might want to consider one of the other existing validators listed below; check the table for the best match between your opinion on what constitutes an “acceptable e-mail address” and the validator’s.
There are many e-mail address validators for Java already. However, they differ in the exact format they expect and validate. Prior to creating this library, I compared some of them to learn whether any of them implemented the exact validation I needed. Unfortunately, it seemed not.
The table shown below was generated using a program in this repository (with a few of the final extreme examples randomly generated by another program in this repo). It compares the following validators:
- Java: The validator built into the standard
javax.mail.internet.InternetAddress
class. - HTML5: The regular expression for validating e-mail input fields in HTML5 (see the HTML5 specification).
- 5322: A random regex I found on StackOverflow, claiming to validate according to RFC 5322.
- CoVa: Apache Commons implementation in
org.apache.commons.validator.routines.EmailValidator
. - EV4j: Validator from egulias/EmailValidator4J.
- EV4j!: The same validator configured as more strict (with
WarningsNotAllowed
).
- EV4j!: The same validator configured as more strict (with
- 2822: Validator from bbottema/email-rfc2822-validator.
- 2822+: The same validator configured as more lax (with
EmailAddressCriteria.RFC_COMPLIANT
).
- 2822+: The same validator configured as more lax (with
- 852V: This validator.
String | Java | HTML5 | 5322 | CoVa | EV4j | EV4j! | 2822 | 2822+ | 852V |
---|---|---|---|---|---|---|---|---|---|
[email protected] |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
jsmith@[192.168.2.1] |
✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ❌ | ✅ | ✅ |
[email protected] |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
[email protected] |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
[email protected] |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
much."more\ unusual"@example.com |
❌ | ❌ | ❌ | ✅ | ✅ | ❌ | ❌ | ❌ | ✅ |
very.unusual."@"[email protected] |
❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
very."(),:;<>[]".VERY."very@\\\ \"very"[email protected] |
❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ✅ |
"Abc@def"@example.com |
✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
"Fred Bloggs"@example.com |
✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
"Joe\\Blow"@example.com |
✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
"Abc@def"@example.com |
✅ | ❌ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ |
customer/[email protected] |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
[email protected] |
✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
!def!xyz%[email protected] |
✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
[email protected] |
✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Abc.example.com |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
[email protected] |
❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
[email protected] |
❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
A<at:var at:name="b" />[email protected] |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
a"b(c)d,e:f;g<h>i[j\k][email protected] |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
just"not"[email protected] |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
this is"not\[email protected] |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ |
this\ still\"not\\[email protected] |
❌ | ❌ | ❌ | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
zKcknV|NGv.lI66vR#@X`QcRK4K.R`?NpA.Gc2Kqzue9.%&nb1kGWp/./#Och$RQv |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ | ✅ |
CwblJW{.+^Y}yw@A-+$`.-007%hT.//$40s.[s~o>Otn>tT0mSK\KdZN3].o1i|V |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
|Y^@[W"t\ih1qHw�=~Ja].3 |
❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ❌ | ✅ |
boolean isEmailValid(String str) {
final Rfc822EmailAddressValidator parser = new Rfc822EmailAddressValidator(str);
try {
parser.validate();
return true;
} catch (ParseException e) {
return false;
}
}
The implementation of the library is Copyright © 2021 Petr Kadlec. It is available under the MIT license.