Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Maven Central Javadoc Coverage Status

A modern, fast, zero-dependency library for working with email addresses and performing email address validation in Java.

Built for Java 8 and up.

Try out the algorithm online!

Why JMail?InstallationUsageIP ValidationContributing

Why JMail?

JMail was built mainly because I wanted to tackle the complex problem of email address validation without using Regex. Along the way, JMail became a much better choice than other Java email validation libraries (such as Apache Commons Validator or Jakarta (Javax) Mail Validation) for the following reasons:

  1. JMail is more correct than other libraries. For example, both Apache Commons and Jakarta Mail consider as a valid email address! It clearly is not, as it has two @ characters. JMail correctly considers this address invalid. You can see a full comparison of correctness and try it out for yourself online.

  2. JMail is faster than other libraries by, on average, at least 2x, thanks in part to lack of regex.

  3. JMail has zero dependencies and is very lightweight.

  4. JMail is modern. It is built for Java 8+, and provides many useful methods and data accessors.

Click here for a full report of the differences in correctness and speed between JMail and other libraries.

While JMail is more correct than other libraries, I cannot guarantee that it is 100% correct. Email RFCs are long and complex, and I have likely missed some specific details. Please open an issue if you find an incorrect validation result for a specific email (or even better, a pull request with a fix).

I also highly recommend that you send verification emails to user-provided email addresses. This is the only way to ensure that the email address exists and that the recipient wants that email address to be used.


Add this library as a dependency in your pom.xml:


Or in your build.gradle:

implementation 'com.sanctionco.jmail:jmail:1.3.3'


Standard Email Validation

To perform standard email validation, use the static methods available in JMail. For example, to test validation:

String email = "";

if (JMail.isValid(email)) {
  // Work with your email string

Or to enforce validation, throwing an InvalidEmailException on failure:

String email = "";

try {
  // Work with your email string
} catch (InvalidEmailException) {
  // Handle invalid email

Custom Email Validation

JMail also provides an EmailValidator class that allows for much more customization of what constitutes a valid email address. You can require additional common validation rules, or supply your own. For example:

// In general, you should use JMail.strictValidator()
EmailValidator validator = JMail.strictValidator()
    // Require that the top-level-domain is ".com"
    // Require that the local-part starts with "allowed"
    .withRule(email -> email.localPart().startsWith("allowed"));

boolean valid = validator.isValid("");
boolean invalidWithoutTld = validator.isValid("allowed@test");
boolean invalidWithoutDotCom = validator.isValid("");
boolean invalidWithoutAllowed = validator.isValid("");

The Email Object

JMail also includes an Email object that makes working with email addresses easier. The Email object has the following properties:

Property getter Description Example using test(hello)@(world)
localPart() The local-part of the email address test(hello)
localPartWithoutComments() The local-part of the email address without comments test
domain() The domain of the email address (world)
domainWithoutComments() The domain of the email address without comments
domainParts() A list of the parts of the domain [example, one, com]
identifier() The identifier of the email address, if it has one. null
(For Admin <>, it would be Admin)
comments() A list of the comments in the email address [hello, world]
explicitSourceRoutes() A list of explicit source routes in the address, if present []
(For @1st.relay,@2nd.relay:user@final.domain, it would be [1st.relay, 2nd.relay])
isIpAddress() Whether or not the domain is an IP address false
hasIdentifier() Whether or not the address has an identifier false
topLevelDomain() The TopLevelDomain of the email address, or TopLevelDomain.OTHER if it is unknown TopLevelDomain.DOT_COM

To create a new instance of Email from a string, use the tryParse(String email) method, either the default version or on your own EmailValidator instance:

Optional<Email> parsed = JMail.tryParse("");

Optional<Email> parsed = JMail.validator()

Since tryParse(String email) returns an Optional<Email>, you can do some cool things, such as:

Use a default email address

String email = JMail.tryParse("invalidEmailString")

Send an email if the address is valid

        email -> myEmailService.sendTo(email.toString()),
        () -> log.error("Could not send email to invalid email"));

Get a normalized version of the email address

// Get a normalized email address without any comments
String normalized = JMail.tryParse("admin(comment)")

// normalized == ""

Additional Validation Rules

Disallow IP Address Domain

Although an email with an IP address in the domain is valid, these email addresses are often rejected from mail servers or only used for spam. You can require that your EmailValidator reject all emails with an IP address in the domain:


Note: JMail.strictValidator() includes this rule automatically.

Require a Top Level Domain

Although an email address can be a local domain name with no TLD, ICANN highly discourages dotless email addresses. You can require that your EmailValidator reject all emails without a TLD:


Note: JMail.strictValidator() includes this rule automatically.

Disallow Explicit Source Routing

Explicit source routing has been deprecated as of RFC 5321 and you SHOULD NOT use explicit source routing except under unusual circumstances.


Note: JMail.strictValidator() includes this rule automatically.

Disallow Reserved Domains

As specified in RFC 2606, some domains are reserved and should not be resolvable. Mail addressed to mailboxes in those reserved domains (and their subdomains) should be non-deliverable. You can require that your EmailValidator reject all emails that have a reserved domain:


Disallow Quoted Identifiers

If you want email addresses to only be the raw email address, use this rule. Adding this will invalidate addresses of the form John Smith <>.


Require a specific common Top Level Domain

You can require that your EmailValidator reject all emails that have a top-level domain other than the ones you specify:

    TopLevelDomain.DOT_NET, TopLevelDomain.DOT_EDU);

Bonus: IP Address Validation

Since validating email addresses requires validation of IP addresses, these IP address validation methods are exposed for your convenience!

Determine if an IP Address is Valid

String ipv4 = "";

if (InternetProtocolAddress.isValid(ipv4)) {
  // Use address
String ipv6 = "2001:db8::1234:5678";

if (InternetProtocolAddress.isValid(ipv6)) {
  // Use address

Enforce an IP Address to be Valid

String ipv4 = "";

try {
} catch (InvalidAddressException e) {
  // Failure
String ipv6 = "2001:db8::1234:5678";

try {
} catch (InvalidAddressException e) {
  // Failure

Validate and return the IP

String ipv4 = "";

Optional<String> validated = InternetProtocolAddress.validate(ipv4);

// The validate() method allows for convenience such as:
String ip = InternetProtocolAddress
String ipv6 = "2001:db8::1234:5678";

Optional<String> validated = InternetProtocolAddress.validate(ipv6);

// The validate() method allows for convenience such as:
String ip = InternetProtocolAddress


All contributions are welcome! Open issues for bug reports or feature requests. Pull requests with fixes or enhancements are encouraged.