@@ -36,10 +36,10 @@ following reasons:
36
36
email address! It clearly is not, as it has two ` @ ` characters. JMail correctly
37
37
considers this address invalid. You can
38
38
[ see a full comparison of correctness and try it out for yourself online] ( https://www.rohannagar.com/jmail/ ) .
39
-
39
+
40
40
2 . JMail is ** _ faster_ ** than other libraries by, on average, at least
41
41
2x, thanks in part to lack of regex.
42
-
42
+
43
43
3 . JMail has ** _ zero dependencies_ ** and is very lightweight.
44
44
45
45
4 . 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`:
65
65
<dependency >
66
66
<groupId >com.sanctionco.jmail</groupId >
67
67
<artifactId >jmail</artifactId >
68
- <version >1.6.2 </version >
68
+ <version >2.0.0 </version >
69
69
</dependency >
70
70
```
71
71
72
72
Or in your ` build.gradle ` :
73
73
74
74
``` groovy
75
- implementation 'com.sanctionco.jmail:jmail:1.6.2 '
75
+ implementation 'com.sanctionco.jmail:jmail:2.0.0 '
76
76
```
77
77
78
78
## Usage
79
79
80
80
### Standard Email Validation
81
81
82
- To perform standard email validation, use the static methods
82
+ To perform standard, RFC-compliant email validation, you can use the static methods
83
83
available in ` JMail ` . For example, to test validation:
84
84
85
85
``` java
@@ -133,7 +133,8 @@ EmailValidator validator = JMail.strictValidator()
133
133
// Require that the top-level-domain is ".com"
134
134
.requireOnlyTopLevelDomains(TopLevelDomain . DOT_COM )
135
135
// 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" ));
137
138
138
139
boolean valid
= validator
. isValid(
" [email protected] " );
139
140
boolean invalidWithoutTld = validator. isValid(" allowed@test" );
193
194
() - > log. error(" Could not send email to invalid email" ));
194
195
```
195
196
196
- #### Get a normalized version of the email address
197
+ #### Get different versions of the email address
197
198
198
199
``` java
199
- // Get a normalized email address without any comments
200
+ // Get a normalized email address
200
201
Optional<String > normalized = JMail . tryParse(" admin(comment)@mysite.org" )
201
202
.map(Email :: normalized);
202
203
203
204
// normalized == Optional.of("[email protected] ")
204
205
```
205
206
206
207
``` 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(
" [email protected] " )
210
+ .map(e - > e. normalized(
211
+ NormalizationOptions . builder()
212
+ .removeSubAddress()
213
+ .build()));
211
214
212
215
// normalized == Optional.of("[email protected] ")
213
216
```
214
217
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(
" [email protected] " )
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(
" [email protected] " )
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(
" [email protected] " )
237
+ .map(Email :: munged);
238
+
239
+ // redacted == Optional.of("te*****@gm*****");
240
+ ```
217
241
218
242
### Additional Validation Rules
219
243
@@ -325,6 +349,17 @@ other than ASCII characters.
325
349
JMail . validator(). requireAscii();
326
350
```
327
351
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
` [email protected] ` valid, even though it is not
357
+ actually valid according to RFC.
358
+
359
+ ``` java
360
+ JMail . validator(). allowNonstandardDots();
361
+ ```
362
+
328
363
### Bonus: IP Address Validation
329
364
330
365
Since validating email addresses requires validation of IP addresses,
@@ -379,8 +414,8 @@ Optional<String> validated = InternetProtocolAddress.validate(ipv4);
379
414
380
415
// The validate() method allows for convenience such as:
381
416
String ip = InternetProtocolAddress
382
- .validate(" notvalid" )
383
- .orElse(" 0.0.0.0" );
417
+ .validate(" notvalid" )
418
+ .orElse(" 0.0.0.0" );
384
419
```
385
420
386
421
``` java
@@ -390,8 +425,8 @@ Optional<String> validated = InternetProtocolAddress.validate(ipv6);
390
425
391
426
// The validate() method allows for convenience such as:
392
427
String ip = InternetProtocolAddress
393
- .validate(" notvalid" )
394
- .orElse(" 2001:db8::1234:5678" );
428
+ .validate(" notvalid" )
429
+ .orElse(" 2001:db8::1234:5678" );
395
430
```
396
431
397
432
### Contributing
0 commit comments