@@ -36,10 +36,10 @@ following reasons:
3636 email address! It clearly is not, as it has two ` @ ` characters. JMail correctly
3737 considers this address invalid. You can
3838 [ see a full comparison of correctness and try it out for yourself online] ( https://www.rohannagar.com/jmail/ ) .
39-
39+
40402 . JMail is ** _ faster_ ** than other libraries by, on average, at least
4141 2x, thanks in part to lack of regex.
42-
42+
43433 . JMail has ** _ zero dependencies_ ** and is very lightweight.
4444
45454 . JMail is ** _ modern_ ** . It is built for Java 8+, and provides many
@@ -65,21 +65,21 @@ Add this library as a dependency in your `pom.xml`:
6565<dependency >
6666 <groupId >com.sanctionco.jmail</groupId >
6767 <artifactId >jmail</artifactId >
68- <version >1.6.2 </version >
68+ <version >2.0.0 </version >
6969</dependency >
7070```
7171
7272Or in your ` build.gradle ` :
7373
7474``` groovy
75- implementation 'com.sanctionco.jmail:jmail:1.6.2 '
75+ implementation 'com.sanctionco.jmail:jmail:2.0.0 '
7676```
7777
7878## Usage
7979
8080### Standard Email Validation
8181
82- To perform standard email validation, use the static methods
82+ To perform standard, RFC-compliant email validation, you can use the static methods
8383available in ` JMail ` . For example, to test validation:
8484
8585``` java
@@ -133,7 +133,8 @@ EmailValidator validator = JMail.strictValidator()
133133 // Require that the top-level-domain is ".com"
134134 .requireOnlyTopLevelDomains(TopLevelDomain . DOT_COM )
135135 // Require that the local-part starts with "allowed"
136- .withRule(email - > email. localPart(). startsWith(" allowed" ));
136+ .withRule(email - > email. localPart(). startsWith(" allowed" ),
137+ new FailureReason (" DOES_NOT_START_WITH_ALLOWED" ));
137138
138139boolean valid = validator. isValid(" allowed-email@test.com" );
139140boolean invalidWithoutTld = validator. isValid(" allowed@test" );
@@ -193,27 +194,50 @@ JMail.tryParse("test@example.com")
193194 () - > log. error(" Could not send email to invalid email" ));
194195```
195196
196- #### Get a normalized version of the email address
197+ #### Get different versions of the email address
197198
198199``` java
199- // Get a normalized email address without any comments
200+ // Get a normalized email address
200201Optional<String > normalized = JMail . tryParse(" admin(comment)@mysite.org" )
201202 .map(Email :: normalized);
202203
203204// normalized == Optional.of("admin@mysite.org")
204205```
205206
206207``` java
207- // Get a normalized email address and strip quotes if the address would
208- // still be valid
209- Optional<String > normalized = JMail . tryParse(" \" test.1\" @mysite.org" )
210- .map(e - > e. normalized(true ));
208+ // Get a normalized email address and remove any sub-addressing when normalizing
209+ Optional<String > normalized = JMail . tryParse(" test.1+mytag@mysite.org" )
210+ .map(e - > e. normalized(
211+ NormalizationOptions . builder()
212+ .removeSubAddress()
213+ .build()));
211214
212215// normalized == Optional.of("test.1@mysite.org")
213216```
214217
215- > ** Note:** You can also set the ` -Djmail.normalize.strip.quotes=true ` JVM property to
216- strip quotes when calling ` normalized() ` without parameters.
218+ ``` java
219+ // Get a reference (MD5 hash) of the email address
220+ Optional<String > reference = JMail . tryParse(" test@gmail.com" )
221+ .map(Email :: reference);
222+
223+ // redacted == Optional.of("1aedb8d9dc4751e229a335e371db8058");
224+ ```
225+
226+ ``` java
227+ // Get a redacted version of the email address
228+ Optional<String > redacted = JMail . tryParse(" test@gmail.com" )
229+ .map(Email :: redacted);
230+
231+ // redacted == Optional.of("{a94a8fe5ccb19ba61c4c0873d391e987982fbbd3}@gmail.com");
232+ ```
233+
234+ ``` java
235+ // Get a munged version of the email address
236+ Optional<String > redacted = JMail . tryParse(" test@gmail.com" )
237+ .map(Email :: munged);
238+
239+ // redacted == Optional.of("te*****@gm*****");
240+ ```
217241
218242### Additional Validation Rules
219243
@@ -325,6 +349,17 @@ other than ASCII characters.
325349JMail . validator(). requireAscii();
326350```
327351
352+ #### Allow Nonstandard Dots in the Local-Part
353+
354+ While technically disallowed under published RFCs, some email providers (ex: GMail)
355+ consider email addresses that have local-parts that start with or end with a dot ` . ` character
356+ as valid. For example, GMail considers ` .my.email.@gmail.com ` valid, even though it is not
357+ actually valid according to RFC.
358+
359+ ``` java
360+ JMail . validator(). allowNonstandardDots();
361+ ```
362+
328363### Bonus: IP Address Validation
329364
330365Since validating email addresses requires validation of IP addresses,
@@ -379,8 +414,8 @@ Optional<String> validated = InternetProtocolAddress.validate(ipv4);
379414
380415// The validate() method allows for convenience such as:
381416String ip = InternetProtocolAddress
382- .validate(" notvalid" )
383- .orElse(" 0.0.0.0" );
417+ .validate(" notvalid" )
418+ .orElse(" 0.0.0.0" );
384419```
385420
386421``` java
@@ -390,8 +425,8 @@ Optional<String> validated = InternetProtocolAddress.validate(ipv6);
390425
391426// The validate() method allows for convenience such as:
392427String ip = InternetProtocolAddress
393- .validate(" notvalid" )
394- .orElse(" 2001:db8::1234:5678" );
428+ .validate(" notvalid" )
429+ .orElse(" 2001:db8::1234:5678" );
395430```
396431
397432### Contributing
0 commit comments