How to validate email in Java without regex?

Member

by wilhelm , in category: Java , a year ago

How to validate email in Java without regex?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by alfonzo , a year ago

@wilhelm 

One way to validate an email address in Java without using a regular expression is to use the javax.mail.internet.InternetAddress class, which provides a method called isValid. This method returns true if the given email address is a valid format, and false if it is not. Here is an example of how you can use this method to validate an email address:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import javax.mail.internet.InternetAddress;

public class EmailValidator {
    public static boolean isValidEmail(String email) {
        try {
            InternetAddress internetAddress = new InternetAddress(email);
            internetAddress.validate();
            return true;
        } catch (AddressException e) {
            return false;
        }
    }
}


You can then use this method to validate an email address like this:

1
2
3
4
5
6
String email = "[email protected]";
if (EmailValidator.isValidEmail(email)) {
    System.out.println("Email is valid");
} else {
    System.out.println("Email is not valid");
}


Keep in mind that this method only checks the syntax of the email address, and does not verify that the email address actually exists. To do that, you would need to send a message to the email address and see if it bounces.

Member

by jermain , 4 months ago

@wilhelm 

Please note that my previous response is incomplete and somewhat inaccurate. The javax.mail.internet.InternetAddress class is primarily intended for sending and parsing email addresses, not for validating them.


To validate an email address in Java without using regular expressions, you can use the javax.mail.internet.AddressException class together with try-catch blocks to catch the AddressException when the email address is invalid. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public class EmailValidator {
    public static boolean isValidEmail(String email) {
        try {
            InternetAddress internetAddress = new InternetAddress(email);
            internetAddress.validate();
            return true;
        } catch (AddressException e) {
            return false;
        }
    }
}

// Usage:
String email = "[email protected]";
if (EmailValidator.isValidEmail(email)) {
    System.out.println("Email is valid");
} else {
    System.out.println("Email is not valid");
}


This code attempts to create an InternetAddress object from the given email string. If there is a parsing exception thrown by the InternetAddress constructor, it means that the email address is invalid, and isValidEmail will return false. Otherwise, if no exception is thrown, the email address is considered valid and isValidEmail will return true.


Again, it's important to note that this validation method only checks the syntax of the email address, and not whether it actually exists or is deliverable.