Verifying email addresses


I've been working on the app that manages workshop registrations here in the Learning Commons, and have been tackling one of the major problems in the current version - people can enter invalid email addresses, and may never even know they did it. They won't receive confirmation, and can't receive updates/modifications.

Often, these addresses are trivially incorrect (an errant space, a missing . or whatever).

Anyway, here is the code I whipped up for the app to attempt to verify the email address. It relies on the Java InetAddress class, and does a lookup of the hostname. If there's a machine at the hostname, I'll assume it's correct. (could get fancy and look up only MX records etc... but it's a start.)


import java.util.*;
import java.net.*;

...

try {
NSLog.out.appendln("Validating email address...");

// get the hostname from the email address
StringTokenizer st = new StringTokenizer(_email, "@");
String accountName = st.nextToken();
String hostName = st.nextToken();

InetAddress emailhost = InetAddress.getByName(domainName);
NSLog.out.appendln("mailhost IP: " + emailhost.getHostAddress());

} catch (UnknownHostException e) {
NSLog.out.appendln("UnknownHostException: " + e.getMessage());
comment = "This email address appears to be invalid. Please verify your email address";

} catch (SecurityException e) {
NSLog.out.appendln("SecurityException: " + e.getMessage());
comment = "This email address appears to be invalid. Please verify your email address";

} catch (Exception e) {
NSLog.out.appendln("Unknown Exception: " + e.getMessage());
comment = "This email address appears to be invalid. Please verify your email address";

}


java 

See Also

comments powered by Disqus