Skip to content

Java implementation of e-mail address validation according strictly to RFC 822

License

Notifications You must be signed in to change notification settings

mormegil-cz/rfcemailvalidator

Repository files navigation

RFC 822 E-mail Address Validator

Java implementation of e-mail address validation according strictly to RFC 822.

Motivation

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.

Existing validators

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:

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

Java library usage

boolean isEmailValid(String str) {
    final Rfc822EmailAddressValidator parser = new Rfc822EmailAddressValidator(str);
    try {
        parser.validate();
        return true;
    } catch (ParseException e) {
        return false;
    }
}

Author & license

The implementation of the library is Copyright © 2021 Petr Kadlec. It is available under the MIT license.

About

Java implementation of e-mail address validation according strictly to RFC 822

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages