Java | Check if domain of an email address is valid or not

Author - Neha Mishra

To Check if the domain of an email address is valid or not, pass the email address to the below method as an argument and execute it, it will return true or false by checking if the email id you entered has valid domain or not :

private static boolean doLookup( String hostName )
String[] emailDN=hostName.split("@");
// System.out.println(emailDN[0]+"	"+emailDN[1]);
Hashtable env = new Hashtable();
DirContext ictx = new InitialDirContext( env );
Attributes attrs = ictx.getAttributes( emailDN[1], new String[] { "MX" });
Attribute attr = attrs.get( "MX" );
if( attr == null )
//return( attr.size() );
return true;
catch (NamingException e) {
// TODO Auto-generated catch block
return false;
public static void main(String[] args){
if(!doLookup("Input email address")){
System.out.printLn(“Invalid domain”);

Input :
Output : Invalid domain

Input :
Output : Valid

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

One response on “Java | Check if domain of an email address is valid or not

  1. Senthi says:

    This code is not checking valid domain. It always return invalid domain even the domain is corrent. I tried gmail ids.

Leave a Reply

Your email address will not be published.