public class Test{
創(chuàng)新互聯(lián)主營藍田網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,手機APP定制開發(fā),藍田h5微信平臺小程序開發(fā)搭建,藍田網(wǎng)站營銷推廣歡迎藍田等地區(qū)企業(yè)咨詢
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;
}
//檢查一個字符串是否包含字母和數(shù)字
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();
}
//?連接到郵箱服務(wù)器
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
郵箱驗證的實現(xiàn),不過是用正則表達式子實現(xiàn)的,能夠滿足你上面提出的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("是網(wǎng)易郵箱"); else System.out.println("不是網(wǎng)易郵箱"); } else System.out.println("根本就不是郵箱"); }