-
By default,
Email#normalized
now lowercases the email address and removes any extraneous quotes in the local-part of the address. To revert this behavior so that it behaves the same as v1, use the following:myEmailObject.normalized( NormalizationOptions.builder() .keepQuotes() .adjustCase(CaseOption.NO_CHANGE) .build());
-
The
jmail.normalize.strip.quotes
JVM system property no longer does anything. Quotes are stripped by default now. If you need to disable quote stripping, useNormalizationOptionsBuilder#keepQuotes()
. -
Removed
Email#normalized(boolean)
method which allowed for a normalized email with stripped quotes. Quotes are stripped by default now. If you need to disable quote stripping, useNormalizationOptionsBuilder#keepQuotes()
. -
FailureReason
was switched from an enum to a class in order to support custom failure reasons, so it is no longer possible to use it in aswitch
statement. -
Email addresses that fail validation due to additional rules added to the
EmailValidator
(such asdisallowIpDomain()
orrequireValidMXRecord()
) no longer return a genericFailureReason.FAILED_CUSTOM_VALIDATION
in theEmailValidationResult
. Instead, it returns a more specificFailureReason
depending on the rule. -
FailureReason.MISSING_TOP_LEVEL_DOMAIN
was changed toFailureReason.MISSING_FINAL_DOMAIN_PART
.MISSING_TOP_LEVEL_DOMAIN
was previously used for email addresses that failed validation because they ended the email address with a comment. ThisFailureReason
was potentially misleading, for example if you enabledrequireTopLevelDomain()
on yourEmailValidator
. Note that theMISSING_TOP_LEVEL_DOMAIN
failure reason is now used properly: if you use the rulerequireTopLevelDomain()
, any address that is missing the TLD will give that failure reason.
The FailureReason
returned in the EmailValidationResult
is useful to understand why a specific
email address failed validation. In v2.0.0, the FailureReason
returned for email addresses that failed
one of the additional validation rules added to your EmailValidator
(such as disallowIpDomain()
or
requireValidMXRecord()
) now return more specific and useful reasons (such as CONTAINS_IP_DOMAIN
or
INVALID_MX_RECORD
).
EmailValidator validator = JMail.strictValidator()
.requireOnlyTopLevelDomains(TopLevelDomain.DOT_COM);
EmailValidationResult result = validator.validate("[email protected]");
assertEquals(FailureReason.INVALID_TOP_LEVEL_DOMAIN, result.getFailureReason());
Additionally, you can specify your own FailureReason
for any custom validation rules that you add
to your EmailValidator
. Use the new withRule(Predicate<Email>, FailureReason)
or
withRules(Map<Predicate<Email>, FailureReason>)
methods to specify the failure reason for each
of your custom rules. If no failure reason is supplied, then the rule will default to the
FailureReason.FAILED_CUSTOM_VALIDATION
reason.
FailureReason nonGmailFailure = new FailureReason("NON_GMAIL_ADDRESS");
EmailValidator validator = JMail.strictValidator()
.withRule(e -> e.domain.startsWith("gmail"), nonGmailFailure);
EmailValidationResult result = validator.validate("[email protected]");
assertEquals(nonGmailFailure, result.getFailureReason());
This version introduces a new NormalizationOptions
class that is used to provide
configuration of the behavior of the Email#normalized()
method. See the table below to see
all the new and existing options.
In v2.0.0, you can use either Email#normalized()
(with no parameters) or Email#normalized(NormalizationOptions options)
.
The first method without parameters will return a normalized email address based on the default
normalization options. The second method allows you to provide your own NormalizationOptions
at runtime
depending on your needs. The custom normalization options can be created using the NormalizationOptions#builder()
method.
Thanks, @Sprokof, for contributing (introducing removeDots
and adjustCase
options)! 🎉
Option | Description | NormalizationOptionsBuilder Method |
---|---|---|
stripQuotes | Remove all unnecessary quotes in the local-part of the address | stripQuotes() |
adjustCase | Adjust the case of the email address. Possible options are: NO_CHANGE , LOWERCASE , LOWERCASE_LOCAL_PART_ONLY , LOWERCASE_DOMAIN_ONLY , UPPERCASE , UPPERCASE_LOCAL_PART_ONLY , UPPERCASE_DOMAIN_ONLY |
adjustCase(CaseOption) |
removeDots | Remove all dots from the local-part of the address | removeDots() |
removeSubAddress | Remove any sub-addressing (or tagged-addressing) from the local-part of the address. For example, [email protected] will become [email protected] |
removeSubAddress() or removeSubAddress(String) |
performUnicodeNormalization | Perform unicode normalization on the local-part of the email address | performUnicodeNormalization() or performUnicodeNormalization(Normalizer.Form) |
Version 2.0.0 introduces new additional email address formats that can be obtained from
the Email
object (similar to the normalized()
method).
-
Email#reference()
returns an MD5 hash of the normalized email address."[email protected]" => "1aedb8d9dc4751e229a335e371db8058"
-
Email#redacted()
returns a version of the normalized email address where the local-part is replaced with the SHA-1 hash of the local-part."[email protected]" => "{a94a8fe5ccb19ba61c4c0873d391e987982fbbd3}@gmail.com"
-
Email#munged()
returns a version of the normalized email address where the local-part and domain are obfuscated with five asterisk characters."[email protected]" => "te*****@gm*****"
While technically disallowed under published RFCs, some email providers (ex: GMail)
consider email addresses that have local-parts that start with or end with a dot .
character
as valid. For example, GMail considers [email protected]
valid, even though it is not
actually valid according to RFC.
JMail now gives you the option to consider these addresses valid as well. You must use an
EmailValidator
with the allowNonstandardDots
rule added to it to allow these addresses to pass validation.
EmailValidator validator = JMail.strictValidator()
.allowNonstandardDots();
validator.isValid("[email protected]"); // returns true
- Fix bug where email addresses containing control characters in the local-part were incorrectly considered valid. (Thanks @PascalSchumacher for reporting!)
- Add new methods
ifValid(Consumer<Email> action)
andifValidOrElse(Consumer<Email> action, Consumer<FailureReason> failureAction)
to theEmailValidationResult
object.
- Fix bug where IPv4 addresses with non-arabic numerals would incorrectly be considered valid. (Thanks @harrel56 for reporting!)
- Fix bug where IPv4 addresses with extraneous leading zeros would incorrectly be considered valid. (Thanks @harrel56 for reporting!)
- The
requireValidMXRecord()
validation rule now correctly fails validation for domains that use a "Null MX" record. (Thanks @elmolm for contributing! 🎉)
- Fix bug so that email addresses that end in a dash
-
character now correctly fail validation with the reasonFailureReason.DOMAIN_PART_ENDS_WITH_DASH
instead of incorrectly returningFailureReason.ENDS_WITH_DOT
. (Thanks @tbatchlear for reporting!)
- Add a new rule
requireAscii()
that considers an email address containing non-ASCII characters to be invalid. (Thanks @frodeto for suggesting!) - Add new property
isAscii()
onEmail
objects that returns if the email address only contains ASCII characters or not. - Add option to strip quotes within the local-part of an email address when normalizing the address with the
normalize()
method. (Thanks @tdelaney-leadiro for suggesting!)- This new option will remove quotes if the email address would still be valid and semantically the same without them.
- To enable the option, either:
- Call the normalize method that takes a boolean as the parameter, and use
true
. Example:email.normalize(true)
- Set the
-Djmail.normalize.strip.quotes=true
JVM property at runtime, and continue to use thenormalize()
method without parameters.
- Call the normalize method that takes a boolean as the parameter, and use
- Add a new rule
requireValidMXRecord(int initialTimeout, int numRetries)
that allows for customization of the timeout for DNS lookups. (Thanks @dotneutron for suggesting!) - Reduce the default timeout for DNS lookups when adding the
requireValidMXRecord()
rule to anEmailValidator
from potentially taking a maximum of 25 seconds to a maximum of 600 milliseconds.
- Add new method
validate(String email)
that returns anEmailValidationResult
object, containing the reason for validation failure upon failure. (Thanks @bobharner for suggesting!) - Add new
ValidationRule
requireValidMXRecord()
to consider email addresses that have a domain with no MX record in DNS as invalid. (Thanks @lpellegr for suggesting!) - Fix bug where an email address that ends with a comment that is missing the closing parentheses were incorrectly considered as valid. For example:
[email protected](comment
- Add new
ValidationRule
disallowObsoleteWhitespace()
to consider email addresses with obsolete whitespace as invalid. (Thanks @PascalSchumacher for suggesting!)
- Add new
normalized()
method on theEmail
class to provide a way to get a "normalized" version of an email address (the address without any comments or optional parts).
- Fix bug where invalid characters in the domain could result in an
IllegalArgumentException
instead of returning false. (Thanks @PascalSchumacher for reporting!)
- Fix bug where domain names that contained an emoji would be incorrectly invalid. (Thanks @Autom8edChaos for reporting!)
- Improve
equals()
andhashCode()
methods forEmail
andTopLevelDomain
- Fix inconsistencies in some Javadocs
InternetProtocolAddress.validate(String ip)
now validates IPv6 addresses without requiring theIPv6:
prefix.- Add new
JMail.isInvalid(String email)
andEmailValidator#isInvalid(String email)
methods as a convenience for testing if an email address is invalid.
- Add
toString()
method onEmailValidator
- Add
withRules(Collection<Predicate<Email>> rules)
method onEmailValidator
to create a newEmailValidator
from the collection of rules provided
- Fix bug where an exception would be thrown on invalid email addresses with whitespace or comments after a trailing
.
character. For example,abc.def@ghi. (comment)
is invalid, and before this version JMail would throw an exception instead of return invalid. (Thanks @ea234 for reporting!)
EmailValidator
is now immutable
- Switch
TopLevelDomain
from an enum to a class, allowing for creation of any valid top level domain (Thanks @bowbahdoe!) - Add
module-info.java
so projects on JDK 9+ can use this library as a Java module - Bugfix: Addresses with empty quoted strings (
""@test.org
) are now correctly considered valid - Bugfix: Addresses with explicit source routing (
@1st.relay,@2nd.relay:[email protected]
) are now considered valid. However, explicit source routing is deprecated since RFC 5321.JMail.strictValidator()
disallows explicit source routing by default - Bugfix: Addresses with quoted identifiers (
John Smith <[email protected]>
) are now correctly considered valid - New properties on the
Email
object:identifier()
hasIdentifier()
explicitSourceRoutes()
- Disallow construction of utility classes and prevent classes from being subclassed (Thanks @bowbahdoe!)
- Fix bug where email addresses that have a dotless domain or top level domain starting with the
-
character would be incorrectly classified as valid. For example,test@-foo
and[email protected]
should both be invalid.
-
You can now disallow email addresses with reserved domains listed in RFC 2606, such as
example.com
or.invalid
.JMail.validator().disallowReservedDomains().isValid("[email protected]");
- Fix bug where JMail did not consider single quoted symbols (ex.
\@
) as valid.
- Better javadocs
- Internal performance improvements
- Add
JMail.strictValidator()
that has pre-configured common rules enabled (stricter than the RFCs allow)
- Initial release of JMail with email validation, IP address validation, and custom rules.