Ed S
Ed S

Reputation: 249

Checking email addresses with InternetAddress.validate()

Why does the following code think that "hurt123@aol-com" is OK?

    public boolean validateEmail(String email) {
    boolean isValid = false;
    try {
        // Create InternetAddress object and validated the supplied
        // address which is this case is an email address.
        InternetAddress internetAddress = new InternetAddress(email, true); // strict
        internetAddress.validate();
        isValid = true;
    } catch (AddressException e) {
        System.out.println("Bad eMail address: " + email);
    }
    return isValid;
}

Upvotes: 1

Views: 3700

Answers (1)

Bill Nguyen
Bill Nguyen

Reputation: 56

isValid = true because hurt123@aol-com is a valid email address.

Although ICANN "highly discourages dotless email addresses", it is still a valid email address. The format of email addresses is local-part@domain where the local part may be up to 64 octets long and the domain may have a maximum of 255 octets (REF)

As long as the local-part and the domain are valid, it is a valid email address.

domain may be an IP address literal, surrounded by square brackets [], such as jsmith@[192.168.2.1] or jsmith@[IPv6:2001:db8::1]

For example: user1@localhost is a valid email address.

Upvotes: 4

Related Questions