JAVA验证邮箱是否有效,所用jar:commons-net-2.,dnsjava-2.1.
public static boolean checkEmail1(String email) {
if (!("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
return false;
}
String log = "";
String host = "";
String hostName = ("@")[1];// 去掉@后面的
//输出邮箱域名
("hostName:" + hostName);
Record[] result = null;
SMTPClient client = new SMTPClient();
//设置超时时间
(8000);
try {
// 查找DN的SMX记录
lookup = new (hostName, );
();
if (() != ) {
("找不到MX记录");
return false;
} else {
result = ();
//循环打印DNS服务器
for (int i = 0; i < ; i++) {
(result[i].getAdditionalName().toString());
}
}
// 循环连接到邮箱DNS服务器
for (int i = 0; i < ; i++) {
host = result[i].getAdditionalName().toString();
//IP地址
//("ip:"+(host).getHostAddress());
int count=0;
try {
(host);//捕获超时异常
} catch (Exception e) {
count++;
//
if(count>=){
return false;
}
}
if (!(())) {
();
continue;
} else {
log += "邮箱mx记录" + hostName + "存在";
log += "成功连接到" + host;
break;
}
}
("");
("@");// 发件人
log += "=" + ();
(email);//发送邮件测试邮箱地址是否存在
log += "\n";
log += "=" + ();
(log+" code"+());
if (250 == ()) {
return true;
}
} catch (Exception e) {
();
} finally {
try {
();
} catch (IOException e) {
}
}
return false;
}