Java验证邮箱是否真实存在有效

Java验证邮箱是否真实存在有效
要检测邮箱是否真实存在,必须了解两方面知识:
1. MX记录,winodws的nslookup命令。 查看学习
2. SMTP协议,如何通过telnet发送邮件。 查看学习

有个网站可以校验, http://verify-email.org/, 不过一小时只允许验证10次。

直接上代码:
JAVA CODE :CheckEmailObj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.IOException;

import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.Type;

public class CheckEmailObj {
public static boolean checkEmail(String email) {
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
System.err.println("Format error");
return false;
}

String log = "";
String host = "";
String hostName = email.split("@")[1];
Record[] result = null;
SMTPClient client = new SMTPClient();

try {
// 查找MX记录
Lookup lookup = new Lookup(hostName, Type.MX);
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL) {
log += "找不到MX记录\n";
return false;
} else {
result = lookup.getAnswers();
}

// 连接到邮箱服务器
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 record about " + hostName + " exists.\n";
log += "Connection succeeded to " + host + "\n";
break;
}
}
log += client.getReplyString();

// HELO cyou-inc.com
client.login("cyou-inc.com");
log += ">HELO cyou-inc.com\n";
log += "=" + client.getReplyString();

// MAIL FROM:
client.setSender("[email protected]");
log += ">MAIL FROM: \n";
log += "=" + client.getReplyString();

// RCPT TO: <$email>
client.addRecipient(email);
log += ">RCPT TO: <" + email + ">\n";
log += "=" + client.getReplyString();

if (250 == client.getReplyCode()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
}
// 打印日志
System.err.println(log);
}
return false;
}

public static void main(String[] args) {
System.err.println("Outcome: "
+ CheckEmailObj.checkEmail("[email protected]"));
}
}
import java.io.IOException; import org.apache.commons.net.smtp.SMTPClient; import org.apache.commons.net.smtp.SMTPReply; import org.xbill.DNS.Lookup; import org.xbill.DNS.Record; import org.xbill.DNS.Type; public class CheckEmailObj { public static boolean checkEmail(String email) { if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) { System.err.println("Format error"); return false; } String log = ""; String host = ""; String hostName = email.split("@")[1]; Record[] result = null; SMTPClient client = new SMTPClient(); try { // 查找MX记录 Lookup lookup = new Lookup(hostName, Type.MX); lookup.run(); if (lookup.getResult() != Lookup.SUCCESSFUL) { log += "找不到MX记录\n"; return false; } else { result = lookup.getAnswers(); } // 连接到邮箱服务器 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 record about " + hostName + " exists.\n"; log += "Connection succeeded to " + host + "\n"; break; } } log += client.getReplyString(); // HELO cyou-inc.com client.login("cyou-inc.com"); log += ">HELO cyou-inc.com\n"; log += "=" + client.getReplyString(); // MAIL FROM: client.setSender("[email protected]"); log += ">MAIL FROM: \n"; log += "=" + client.getReplyString(); // RCPT TO: <$email> client.addRecipient(email); log += ">RCPT TO: <" + email + ">\n"; log += "=" + client.getReplyString(); if (250 == client.getReplyCode()) { return true; } } catch (Exception e) { e.printStackTrace(); } finally { try { client.disconnect(); } catch (IOException e) { } // 打印日志 System.err.println(log); } return false; } public static void main(String[] args) { System.err.println("Outcome: " + CheckEmailObj.checkEmail("[email protected]")); } }

需要的两个jar包:
http://www.dnsjava.org/download/ 
http://apache.etoak.com /commons/net/binaries/commons-net-2.0.zip

执行结果:
MX record about gmail.com exists.
Connection succeeded to alt2.gmail-smtp-in.l.google.com.
220 mx.google.com ESMTP p37si6502151gvf.9
>HELO cyou-inc.com
=250 mx.google.com at your service
>MAIL FROM:
=250 2.1.0 OK p37si6502151gvf.9
>RCPT TO:
=250 2.1.5 OK p37si6502151gvf.9

Outcome: true


或者通过webservice实现:
JAVA CODE :CheckEmailWS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

public class CheckEmailWS {
public static boolean checkEmail(String email) {
String soapRequestData = ""
+ ""
+ ""
+ " "
+ " "
+ " " + email + "" + " "
+ " " + "";

try {
URL u = new URL(
"http://www.webservicex.net/ValidateEmail.asmx?op=IsValidEmail");
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type",
"application/soap+xml; charset=utf-8");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(soapRequestData);
pw.close();

DocumentBuilderFactory bf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = bf.newDocumentBuilder();
Document document = db.parse(uc.getInputStream());

String res = document.getElementsByTagName("IsValidEmailResult")
.item(0).getTextContent();

return Boolean.parseBoolean(res);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

你可能感兴趣的:(Java验证邮箱是否真实存在有效)