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

| By Webner

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

One comment

  1. 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. Required fields are marked *