新项目,有需要提前“判断电子邮件地址是否真实存在”。
首先想到这是一个标准化问题,网上肯定有参考答案了。
大概思路是,发一封邮件给这个账户,或者通过SMTP等协议进行通信。
邮箱几十亿,不可能有简单的API,直接判断是否有效,不然全网等可用邮箱都被你给拿到了。
简单做个汇总,备忘。
实践结论:QQ、163等标准化邮箱,可以判断是否存在。部分企业邮箱,如果使用的是腾讯企业邮箱,也可以。
部分企业邮箱,京东等,不太行。
微软Hotmail邮箱,返回超时。
在线检测工具,比如 https://verify-email.org/ 感觉不靠谱。
2037088@qq.com is BAD
从了解到放弃。
实际测试的邮箱,已做修改,仅供参考。
第1种实践过的,最佳代码:
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.MXRecord;
import org.xbill.DNS.Record;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
public class MailValidKit {
public static void main(String[] args) {
System.out.println("应该为true:"+new MailValidKit().valid("100582783@qq.com", "jootmir.org"));
System.out.println("应该为true:"+new MailValidKit().valid("fasuion@qq.com", "jootmir.org"));
System.out.println("应该为false:"+new MailValidKit().valid("fansnion@qq.com", "jootmir.org"));
System.out.println("应该为true:"+new MailValidKit().valid("lewenans@163.com", "jootmir.org"));
System.out.println("应该为true:"+new MailValidKit().valid("wen.lei@10cdit.com", "jootmir.org"));
System.out.println("应该为true:"+new MailValidKit().valid("ein1@jd.com", "jootmir.org"));
System.out.println("应该为true:"+new MailValidKit().valid("wngongao@jd.com", "jootmir.org"));
}
/**
* 验证邮箱是否存在
* <br>
* 由于要读取IO,会造成线程阻塞
*
* @param toMail
* 要验证的邮箱
* @param domain
* 发出验证请求的域名(是当前站点的域名,可以任意指定)
* @return
* 邮箱是否可达
*/
public boolean valid(String toMail, String domain) {
if(StringUtils.isBlank(toMail) || StringUtils.isBlank(domain)) return false;
if(!StringUtils.contains(toMail, '@')) return false;
String host = toMail.substring(toMail.indexOf('@') + 1);
if(host.equals(domain)) return false;
Socket socket = new Socket();
try {
// 查找mx记录
Record[] mxRecords = new Lookup(host, Type.MX).run();
if(ArrayUtils.isEmpty(mxRecords)) return false;
// 邮件服务器地址
String mxHost = ((MXRecord)mxRecords[0]).getTarget().toString();
if(mxRecords.length > 1) { // 优先级排序
List<Record> arrRecords = new ArrayList<Record>();
Collections.addAll(arrRecords, mxRecords);
Collections.sort(arrRecords, new Comparator<Record>() {
public int compare(Record o1, Record o2) {
return new CompareToBuilder().append(((MXRecord)o1).getPriority(), ((MXRecord)o2).getPriority()).toComparison();
}
});
mxHost = ((MXRecord)arrRecords.get(0)).getTarget().toString();
}
// 开始smtp
socket.connect(new InetSocketAddress(mxHost, 25));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(socket.getInputStream())));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
// 超时时间(毫秒)
long timeout = 6000;
// 睡眠时间片段(50毫秒)
int sleepSect = 50;
// 连接(服务器是否就绪)
if(getResponseCode(timeout, sleepSect, bufferedReader) != 220) {
return false;
}
// 握手
bufferedWriter.write("HELO " + domain + "\r\n");
bufferedWriter.flush();
if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) {
return false;
}
// 身份
bufferedWriter.write("MAIL FROM: <check@" + domain + ">\r\n");
bufferedWriter.flush();
if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) {
return false;
}
// 验证
bufferedWriter.write("RCPT TO: <" + toMail + ">\r\n");
bufferedWriter.flush();
if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) {
return false;
}
// 断开
bufferedWriter.write("QUIT\r\n");
bufferedWriter.flush();
return true;
} catch (NumberFormatException e) {
} catch (TextParseException e) {
} catch (IOException e) {
} catch (InterruptedException e) {
} finally {
try {
socket.close();
} catch (IOException e) {
}
}
return false;
}
private int getResponseCode(long timeout, int sleepSect, BufferedReader bufferedReader) throws InterruptedException, NumberFormatException, IOException {
int code = 0;
for(long i = sleepSect; i < timeout; i += sleepSect) {
Thread.sleep(sleepSect);
if(bufferedReader.ready()) {
String outline = bufferedReader.readLine();
// FIXME 读完……
while(bufferedReader.ready())
/*System.out.println(*/bufferedReader.readLine()/*)*/;
/*System.out.println(outline);*/
code = Integer.parseInt(outline.substring(0, 3));
break;
}
}
return code;
}
}
代码主要参考:https://www.cnblogs.com/Johness/p/javaemail_usesockettocheckemailaddressvalid.html
第2种,还可以,163邮箱无法识别
import java.io.IOException;import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.apache.log4j.Logger;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;
/**
*
* 校验邮箱:1、格式是否正确 2、是否真实有效的邮箱地址
* 步骤: 1、从dns缓存服务器上查询邮箱域名对应的SMTP服务器地址 2、尝试建立Socket连接
* 3、尝试发送一条消息给SMTP服务器 4、设置邮件发送者 5、设置邮件接收者 6、检查响应码是否为250(为250则说明这个邮箱地址是真实有效的)
*
* @author Michael Ran
*
*/
// 总体靠谱
public class CheckEmailValidityUtil {
private static final Logger logger = Logger.getLogger(CheckEmailValidityUtil.class);
/**
* @param email
* 待校验的邮箱地址
* @return
*/
public static boolean isEmailValid(String email) {
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
logger.error("邮箱(" + email + ")校验未通过,格式不对!");
return false;
}
String host = "";
String hostName = email.split("@")[1];
// Record: A generic DNS resource record. The specific record types
// extend this class. A record contains a name, type, class, ttl, and
// rdata.
Record[] result = null;
SMTPClient client = new SMTPClient();
try {
// 查找DNS缓存服务器上为MX类型的缓存域名信息
Lookup lookup = new Lookup(hostName, Type.MX);
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL) {// 查找失败
logger.error("邮箱(" + email + ")校验未通过,未找到对应的MX记录!");
return false;
} else {// 查找成功
result = lookup.getAnswers();
}
// 尝试和SMTP邮箱服务器建立Socket连接
for (int i = 0; i < result.length; i++) {
host = result[i].getAdditionalName().toString();
logger.info("SMTPClient try connect to host:" + host);
// 此connect()方法来自SMTPClient的父类:org.apache.commons.net.SocketClient
// 继承关系结构:org.apache.commons.net.smtp.SMTPClient-->org.apache.commons.net.smtp.SMTP-->org.apache.commons.net.SocketClient
// Opens a Socket connected to a remote host at the current
// default port and
// originating from the current host at a system assigned port.
// Before returning,
// _connectAction_() is called to perform connection
// initialization actions.
// 尝试Socket连接到SMTP服务器
client.connect(host);
// Determine if a reply code is a positive completion
// response(查看响应码是否正常%E