//使用正则表达式验证一串以逗号隔开的字符串是否为有效地址,并以数组返回有效地址
private InternetAddress[] formateAddress(String str){
ArrayList<InternetAddress> list =new ArrayList<>();
if(str!=null){
String[] add=str.split(",");
if(add!=null){
for(int i=0;i<add.length;i++){
if(isValidEmailAddress(add[i])){
try{
list.add(new InternetAddress(add[i]));
} catch (AddressException e) {
}
}
}
if(list.size()>0){
InternetAddress[] address=new InternetAddress[list.size()];
list.toArray(address);
return address;
}
return null;
}
}
return null;
}
/**
*1. 必须包含一个并且只有一个符号“@”
*2. 第一个字符不得是“@”或者“.”
*3. 不允许出现“@.”或者.@
*4. 结尾不得是字符“@”或者“.”
*5. 允许“@”前的字符中出现“+”
*6. 不允许“+”在最前面,或者“+@”
*/
public static boolean isValidEmailAddress(String email){
String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(email);
boolean isMatched = matcher.matches();
return isMatched;
}