Jai’s Weblog – Tech, Security & Fun…

Tech, Security & Fun…

  • Jaibeer Malik

    Jaibeer Malik
  • View Jaibeer Malik's profile on LinkedIn
  • Subscribe

  • Feedburner

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 28 other followers

  • Archives

  • Categories

  • Stats

    • 274,960
  • Live Traffic

Email domain validation in java on server side

Posted by Jai on October 10, 2013

Email Address name and domain name validation on client side or server side are import part of maintaining quality customer data. There are plenty of utilities to allow you to validate the valid name formatting for the same. This post covers to check if the domain name used in the given email address actually valid/exists or not using MX records.

Email Address name validation

Email Address regular expression validation covers the valid regular expression matching.

Apache commons library Email validator covers the email name validation in java.

Check How To Validate Email Address With Regular Expression to find the matching regular expression for Email Address.

Email domain MX record validation

The post Java Extended Email Validation Using DNS MX Lookup covers to validate the MX records for the email domain.

dnsjava library allows you do the real dns validation on java side.

Maven dependency


Use below maven dependency for maven integration.

<dependency>
			<groupId>dnsjava</groupId>
			<artifactId>dnsjava</artifactId>
			<version>2.1.1</version>
		</dependency>

Example

Find below code snippet to check for the MX records for the email domain validation.

domainName="wordpress.com"
try
{
SimpleResolver resolver = new SimpleResolver();
resolver.setTimeout(timeoutInSeconds);
Lookup lu = new Lookup(domainName,Type.MX);
lu.setResolver(resolver);
Record[] records = lu.run();
if (logger.isDebugEnabled())
 {
    if (records == null)
    {
       logger.debug("No MX records were found");
    }
    else
    {
        StringBuilder sb = new StringBuilder();
        String seperator = "";
        for(Record record : records)
        {
          sb.append(seperator);
          sb.append(record.getName());
          seperator = ", ";
        }
       logger.debug("Found "+records.length+" MX records: "+sb.toString());
    }
}

if (records != null && records.length > 0)
{
// Found one or more MX records, no need to continue searching...
return true;
}

logger.debug("No MX records found, checking for A records");
lu = new Lookup(domainName,Type.A);
lu.setResolver(resolver);
records = lu.run();
if (logger.isDebugEnabled())
{
    if (records == null)
    {
        logger.debug("No A records were found");
    }
   else
   {
     StringBuilder sb = new StringBuilder();
     String seperator = "";
     for(Record record : records)
     {
         sb.append(seperator);
         sb.append(record.getName());
         seperator = ", ";
     }
    logger.debug("Found "+records.length+" A records: "+sb.toString());
   }
}
return records != null && records.length > 0;
}

In the above code snippet we try to find the MX records first to check if the the record for domainName “wordpress.com” exists. If not, we try to cross check if type “A” record exists. Use timeout functionality not to let the process hanging for check.

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 28 other followers

%d bloggers like this: