Java判断邮箱是否存在 有返回值

public static boolean checkEmail(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();
try {
// 查找MX记录
Lookup lookup = new Lookup(hostName, Type.MX);
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL) {
System.out.println("找不到MX记录");
return false;
} else {
result = lookup.getAnswers();
for (int i = 0; i < result.length; i++) {
System.out
.println(result[i].getAdditionalName().toString());
System.out.println(result[i]);
}
}
// 连接到邮箱服务器
for (int i = 0; i < result.length; i++) {
host = result[i].getAdditionalName().toString();
client.connect(host);
if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
continue;
} else {
log += "邮箱mx记录" + hostName + "存在";
log += "成功连接到" + host;
break;
}
}
System.out.println(client.getReplyString());
client.login("163.com");
System.out.println(client.getReplyString());
client.setSender("www.linuxidc.com@linuxidc.com");// 发件人
log += "=" + client.getReplyString();
client.addRecipient(email);
log += ">RCPT TO: <" + email + ">\n";
log += "=" + client.getReplyString();
if (250 == client.getReplyCode()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.getMessage());
} finally {
try {
client.disconnect();
} catch (IOException e) {
} // 打印日志
}
return false;
}

如果是250的话这个邮箱地址就存在,返回一个true.
不存在则返回false

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwfypg.html