重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
public class Test{
创新互联主营蓝田网站建设的网络公司,主营网站建设方案,手机APP定制开发,蓝田h5微信平台小程序开发搭建,蓝田网站营销推广欢迎蓝田等地区企业咨询
public static void main(String[] args){
Test t = new Test();
String email = "test@sina.com" ;
boolean isEmail = t.isEmail(email);
if(isEmail){
System.out.println(email +" is a right email!!!");
}else{
System.out.println("error");
}
}
public boolean isEmail(String email){
//正则表达式
/*
String regex = "^[A-Za-z]{1,40}@[A-Za-z0-9]{1,40}\\.[A-Za-z]{2,3}$";
return email.matches(regex);
*/
//不适用正则
if(email==null||"".equals(email)) return false ;
if(!containsOneWord('@',email)||!containsOneWord('.',email)) return false;
String prefix = email.substring(0,email.indexOf("@"));
String middle = email.substring(email.indexOf("@")+1,email.indexOf("."));
String subfix = email.substring(email.indexOf(".")+1);
System.out.println("prefix="+prefix +" middle="+middle+" subfix="+subfix);
if(prefix==null||prefix.length()40||prefix.length()==0) return false ;
if(!isAllWords(prefix)) return false ;
if(middle==null||middle.length()40||middle.length()==0) return false ;
if(!isAllWordsAndNo(middle)) return false ;
if(subfix==null||subfix.length()3||subfix.length()2) return false ;
if(!isAllWords(subfix)) return false ;
return true ;
}
//判断字符串只包含指定的一个字符c
private boolean containsOneWord(char c , String word){
char[] array = word.toCharArray();
int count = 0 ;
for(Character ch : array){
if(c == ch) {
count++;
}
}
return count==1 ;
}
//检查一个字符串是否全部是字母
private boolean isAllWords(String prefix){
char[] array = prefix.toCharArray();
for(Character ch : array){
if(ch'A' || ch'z' || (ch'a' ch'Z')) return false ;
}
return true;
}
//检查一个字符串是否包含字母和数字
private boolean isAllWordsAndNo(String middle){
char[] array = middle.toCharArray();
for(Character ch : array){
if(ch'0' || ch 'z') return false ;
else if(ch '9' ch 'A') return false ;
else if(ch 'Z' ch 'a') return false ;
}
return true ;
}
}
首先使用java提供的格式类判断email是否格式有误,然后使用开源框架,验证邮箱是否有用,示例如下:
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
邮箱验证的实现,不过是用正则表达式子实现的,能够满足你上面提出的7条要求 String email = "dfbv423@163.com"; Pattern pattern = Pattern .compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); Matcher matcher = pattern.matcher(email); if (true == matcher.matches()) { if (email.indexOf("@163.com") 0) System.out.println("是网易邮箱"); else System.out.println("不是网易邮箱"); } else System.out.println("根本就不是邮箱"); }