Blog

Managed File Transfer and Network Solutions

Validating Email Addresses with Java

Posted by Charles Ott on Wed, Jan 04, 2012 @ 11:40 AM

email validation and email verificationGetting validation for an email address using JSCAPE's Secure iNet Factory library is a lot easier than getting personal validation (although let me say right now, dear Reader, that I think you're smart to learn more about the EmailInspector class, and you're all right with me!). The EmailInspector class has four levels of validation and you can give a submitted email address only as much scrutiny as it needs. In each case, the EmailInspector returns true if the address passes validation, and throws an exception if it does not.

  • EmailInspector.SYNTAX_VALIDATION: Is this address in valid form? That is, does it have an "@" sign, does it end with ".something" and does it avoid wrong characters such as spaces?
  • EmailInspector.DOMAIN_VALIDATION: Is the domain name real? For this (and the remaining validations), the EmailInspector instance will need the name of your DNS (Domain Name System) server (such as "ns1.fluffysheep.com") to look up the domain and make sure it exists. You get the DNS information from your Internet service provider.
  • EmailInspector.MX_VALIDATION: Given that it's a real domain name, does it have the necessary "MX" (Mail Exchange) record?
  • EmailInspector.USER_VALIDATION: This is the check-everything validation. It checks the syntax, the domain name, and the MX record, and also that a connection can be made to the email server for the given email address.

Here's an example:

Validating an Email Address with EmailInspector

import com.jscape.inspect.*;
import java.io.*;

public class EmailCheck {
	
	public static void main(String[] args) throws Exception {
		String email = null;
		String dns = null;
		
	// This example reads the email address from the console.
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter email address to validate: ");
        email = reader.readLine();
		
	// Your DNS hostname is necessary to verify that a domain name is real.
        System.out.print("Enter DNS hostname to perform domain validation (e.g. ns.myhost.com): ");
        dns = reader.readLine();
		
        // Create an EmailInspector image. Debugging throws a number of messages to the console.
        EmailInspector inspector = new EmailInspector();
        inspector.setDebug(true);
        
        // Set the DNS level and validation level, then validate the address you entered.
        inspector.setNameserver(dns);
        inspector.setEmailInspectionLevel(inspector.USER_VALIDATION);
        inspector.validate(email);
	}
}

Your postman will get the mail to you even through rain or snow or gloom of night, but your email service will conk out if somebody puts, say, an apostrophe in an address. You can and should check every email address before sending a message.

Download Secure iNet Factory

Topics: Secure iNet Factory