最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java对email邮箱的真实、有效性验证的例子
时间:2022-06-29 03:07:19 编辑:袖梨 来源:一聚教程网
三种验证邮箱有效性的方式:
方式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("[email protected]");
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;
}
}
相关文章
- 超级机器人大战Y魔女的心愿怎么过 特殊剧情关卡攻略 09-16
- 无主之地4跨平台联机教程 怎么跨平台联机 09-16
- 原神纳西妲绘想游迹怎么打 纳西妲绘想游迹满星通关思路 09-16
- 超级机器人大战Y六人一匹一体怎么过 特殊合体关卡攻略 09-16
- 无主之地4解压慢怎么办 解压慢的解决方法 09-16
- 原神隐藏成就无声指令怎么解锁 无声指令成就攻略 09-16