Email Validation with Java
Invalid email addresses can be very difficult to detect. More importantly, once an invalid email address gets into your system it can waste valuable system resources and prevent your application from working correctly. While some basic regular expressions can help you to identify syntax errors in an email address it is much more difficult to determine whether the domain portion of the email actually exists and whether it has an active SMTP server.
In the 8.0 release of Secure iNet Factory a new class is available named com.jscape.inspect.EmailInspector that greatly simplifies email address verification. The EmailInspector class provides several levels of email address validation which include:
Syntax Validation
This is the most basic level of validation and verifies only that the syntax of the email address is correct according to rules defined in RFC 822.
Domain Validation
In addition to Syntax Validation, this level of validation verifies that the domain portion of the email address is a valid active domain.
MX Validation
In addition to Domain Validation, this level of validation verifies that one or more MX (Mail Exchanger) record exists for the domain portion of the email address. MX records point to SMTP servers that are responsible for handling mail for a given domain. An MX record must exist in order for a domain to accept email.
User Validation
In addition to MX Validation, this level of validation connects to the SMTP server responsible for receiving mail for the email address and verifies that no error message is returned by SMTP server when attempting to send a message to the email address.
Each level of validation takes some additional time, especially in the case of MX Validation and above where network connections must be established. The EmailInspector class in Secure iNet Factory has however made this process very efficient with email validation times typically less than 1 second regardless of validation level. This process may also be greatly improved by using a DNS caching feature found in EmailInspector class that caches DNS requests.
Code Example
Download EmailInspectorExample.java (1.3K)
01 /*
02 * @(#) EmailInspectorExample.java
03 *
04 * Copyright (c) JSCAPE LLC.
05 *
06 * This software is the confidential and proprietary information of
07 * JSCAPE. ("Confidential Information"). You shall not disclose such
08 * Confidential Information and shall use it only in accordance with
09 * the terms of the license agreement you entered into with JSCAPE.
10 */
11
12 import com.jscape.inspect.*;
13 import java.io.*;
14
15 public class EmailInspectorExample {
16
17 public static void main(String[] args) throws Exception {
18 String email = null;
19 String dns = null;
20
21 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
22 System.out.print("Enter email address to validate: ");
23 email = reader.readLine();
24 System.out.print("Enter DNS hostname to perform domain validation (e.g. ns.myhost.com): ");
25 dns = reader.readLine();
26 // create EmailInspector instance
27 EmailInspector inspector = new EmailInspector();
28
29 // enable debugging
30 inspector.setDebug(true);
31
32 // set DNS server
33 inspector.setNameserver(dns);
34
35 // set validation level
36 inspector.setEmailInspectionLevel(inspector.DOMAIN_VALIDATION);
37
38 // validate email
39 inspector.validate(email);
40 }
41
42 }
- Line 27. Create new EmailInspector instance.
- Line 30. Enable debugging.
- Line 33. Set DNS server to be used for looking up domains.
- Line 36. Set validation level.
- Line 39. Validate email address.
Comments