JAVA验证邮箱是否有效,所用jar:commons-net-2.2.jar,dnsjava-2.1.7.jar
public static boolean checkEmail1(String email) {
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
return false;
}
String log = "";
String host = "";
String hostName = email.split("@")[1];// 去掉@后面的
//输出邮箱域名
System.out.println("hostName:" + hostName);
Record[] result = null;
SMTPClient client = new SMTPClient();
//设置超时时间
client.setConnectTimeout(8000);
try {
// 查找DN的SMX记录
org.xbill.DNS.Lookup lookup = new org.xbill.DNS.Lookup(hostName, Type.MX);
lookup.run();
if (lookup.getResult() != org.xbill.DNS.Lookup.SUCCESSFUL) {
System.out.println("找不到MX记录");
return false;
} else {
result = lookup.getAnswers();
//循环打印DNS服务器
for (int i = 0; i < result.length; i++) {
System.out.println(result[i].getAdditionalName().toString());
}
}
// 循环连接到邮箱DNS服务器
for (int i = 0; i < result.length; i++) {
host = result[i].getAdditionalName().toString();
//IP地址
//System.out.println("ip:"+InetAddress.getByName(host).getHostAddress());
int count=0;
try {
client.connect(host);//捕获超时异常
} catch (Exception e) {
count++;
//
if(count>=result.length){
return false;
}
}
if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
continue;
} else {
log += "邮箱mx记录" + hostName + "存在";
log += "成功连接到" + host;
break;
}
}
client.login("163.com");
client.setSender("www.linuxidc.com@linuxidc.com");// 发件人
log += "=" + client.getReplyString();
client.addRecipient(email);//发送邮件测试邮箱地址是否存在
log += "\n";
log += "=" + client.getReplyString();
System.out.println(log+" code"+client.getReplyCode());
if (250 == client.getReplyCode()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
}
}
return false;
}