java对email邮箱的真实、有效性验证
2016-11-18 12:59
483 查看
三种验证邮箱有效性的方式:
方式1:
public static boolean checkEmail(String email) {
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
return false;
}
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) {
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 {
break;
}
}
//以下2项自己填写快速的,有效的邮箱
client.login("163.com");
client.setSender("sxgkwei@163.com");
client.addRecipient(email);
if (250 == client.getReplyCode()) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
} catch (IOException e) {
}
}
return false;
}
此方式需要的jar支持:commons-net-2.2.jar,dnsjava-2.1.1.jar
方式2:
public static boolean checkEmail(String email) throws Exception {
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
return false;
}
IsEMailResult result = IsEMail.is_email_verbose(email, true);
switch (result.getState()) {
case OK:
return true;
default:
return false;
}
}
此方式需要的jar支持:IsEMail.jar
方式3:直接去邮箱验证网站去做验证。国外比较好的验证邮箱网站:http://verify-email.org/
相关文章推荐
- Java实现通过smtp服务器验证邮箱的真实有效性
- java对email的真实、有效性验证
- [PHP实例] php Socket发送邮件验证邮箱的真实有效性而非格式
- java正则表达式验证IP,日期和邮箱格式有效性
- 使用java email 进行邮箱验证
- Java验证邮箱有效性和真实性
- asp email邮箱地址验证正则表达式
- java 验证邮箱格式正确性、验证字符串是否为数字
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- [导入]Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- asp IsValidEmail 验证邮箱地址函数(email)
- java 验证邮箱格式正确性、验证字符串是否为数字
- VC++验证EMAIL的有效性
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- Java Email简单验证
- 邮箱有效性验证
- java 验证邮箱格式正确性、验证字符串是否为数字
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- Asp.net 2.0 实现自定义Email格式有效性验证(示例代码下载)
- Java,对日期时间的格式和有效性进行验证的一个好办法。