JAVA MAIL之email地址处理
对于email地址的定义,可以参考RFC822,里面有详细的说明。
1、采用正则表达式的方式来验证email地址:
JS处理方式(来自javascript.internet.com的Sandeep V. Tamhankar):
function
checkEmail(emailStr)
{
if (emailStr.length == 0) {
return true;
}
var emailPat=/^(.+)@(.+)$/;
// \(\)<>@,;:\\\"\.\[\]
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
// [^\s\(\)<>@,;:\\\"\.\[\]]
var validChars="\[^\\s" + specialChars + "\]";
//("[^"]*")
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
// [^\s\(\)<>@,;:\\\"\.\[\]]+
var atom=validChars + '+';
// ([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))
var word="(" + atom + "|" + quotedUser + ")";
//^([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))(\.([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*")))*$
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
// ^([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))(\.([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*")))*$
var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray[i] > 255) {
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1].length < 2) ||
(domArr[domArr.length-1].length > 3)) {
return false;
}
if (len < 2) {
return false;
}
return true;
}
if (emailStr.length == 0) {
return true;
}
var emailPat=/^(.+)@(.+)$/;
// \(\)<>@,;:\\\"\.\[\]
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
// [^\s\(\)<>@,;:\\\"\.\[\]]
var validChars="\[^\\s" + specialChars + "\]";
//("[^"]*")
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
// [^\s\(\)<>@,;:\\\"\.\[\]]+
var atom=validChars + '+';
// ([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))
var word="(" + atom + "|" + quotedUser + ")";
//^([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))(\.([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*")))*$
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
// ^([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*"))(\.([^\s\(\)<>@,;:\\\"\.\[\]]+|("[^"]*")))*$
var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray == null) {
return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat) == null) {
return false;
}
var IPArray = domain.match(ipDomainPat);
if (IPArray != null) {
for (var i = 1; i <= 4; i++) {
if (IPArray[i] > 255) {
return false;
}
}
return true;
}
var domainArray=domain.match(domainPat);
if (domainArray == null) {
return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1].length < 2) ||
(domArr[domArr.length-1].length > 3)) {
return false;
}
if (len < 2) {
return false;
}
return true;
}
下面是我的java实现:
public
static
String SPECIAL_CHARS
=
"
\\(\\)<>@,;:\\\\\\\
"
\\.\\[\\]
"
;
public static String VALID_CHARS = " [^\\s " + SPECIAL_CHARS + " ] " ;
public static String QUOTED_USER = " (\ " [ ^ \ " ]*\ " ) " ;
public static String ATOM = VALID_CHARS + " + " ;
public static String WORD = " ( " + ATOM + " | " + QUOTED_USER + " ) " ;
public static String EMAIL_PATTERN = " ^(.+)@(.+)$ " ;
public static String IP_DOMAIN_PATTERN = " ^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$ " ;
public static String USER_PATTERN = " ^ " + WORD + " (\\. " + WORD + " )*$ " ;
public static String DOMAIN_PATTERN = " ^ " + ATOM + " (\\. " + ATOM + " )*$ " ;
public static String LEGAL_ASCII_PATTERN = " ^[\\0000-\\0177]+$ " ;
public static String SINGLE_EMAIL_PATTERN = " ( " + ATOM + " (\\. " + ATOM + " )*@ " + ATOM
+ " (\\. " + ATOM + " )*) " ;
public static boolean isValid(String input) {
if (input == null) {
return false;
}
if (!Pattern.matches(LEGAL_ASCII_PATTERN, input)) {
return false;
}
Matcher email = Pattern.compile(EMAIL_PATTERN).matcher(input);
if (!email.matches()) {
return false;
}
if (!Pattern.matches(USER_PATTERN, email.group(1))) {
return false;
}
String domain = email.group(2);
Matcher ips = Pattern.compile(IP_DOMAIN_PATTERN).matcher(domain);
if (ips.matches()) {
for (int i = 1; i <= 4; i++) {
if (Integer.parseInt(ips.group(i)) > 255) {
return false;
}
}
return true;
}
if (!Pattern.matches(DOMAIN_PATTERN, domain)) {
return false;
}
return true;
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("[email protected]");
list.add("te%$#[email protected]");
list.add("[email protected]");
list.add("[email protected]");
list.add("te我的家@test.com [email protected]");
list.add("Alfred.Neuman@BBN-TENEXA");
list.add("\"George,Ted\"@Group.Arpanet");
list.add("Wilt(the Stilt)[email protected] [email protected]");
for (String str : list) {
System.out.println(isValid(str));
}
System.out.println();
// 从输入字符串中提取电子邮件地址
Pattern ascill = Pattern.compile(LEGAL_ASCII_PATTERN);
Pattern p = Pattern.compile(SINGLE_EMAIL_PATTERN);
Matcher m = p.matcher("");
String temp = null;
for (String str : list) {
m.reset(str);
while (m.find()) {
temp = m.group();
if (ascill.matcher(temp).matches()) {
System.out.println(temp);
}
}
}
}
public static String VALID_CHARS = " [^\\s " + SPECIAL_CHARS + " ] " ;
public static String QUOTED_USER = " (\ " [ ^ \ " ]*\ " ) " ;
public static String ATOM = VALID_CHARS + " + " ;
public static String WORD = " ( " + ATOM + " | " + QUOTED_USER + " ) " ;
public static String EMAIL_PATTERN = " ^(.+)@(.+)$ " ;
public static String IP_DOMAIN_PATTERN = " ^(\\d{1,3}).(\\d{1,3}).(\\d{1,3}).(\\d{1,3})$ " ;
public static String USER_PATTERN = " ^ " + WORD + " (\\. " + WORD + " )*$ " ;
public static String DOMAIN_PATTERN = " ^ " + ATOM + " (\\. " + ATOM + " )*$ " ;
public static String LEGAL_ASCII_PATTERN = " ^[\\0000-\\0177]+$ " ;
public static String SINGLE_EMAIL_PATTERN = " ( " + ATOM + " (\\. " + ATOM + " )*@ " + ATOM
+ " (\\. " + ATOM + " )*) " ;
public static boolean isValid(String input) {
if (input == null) {
return false;
}
if (!Pattern.matches(LEGAL_ASCII_PATTERN, input)) {
return false;
}
Matcher email = Pattern.compile(EMAIL_PATTERN).matcher(input);
if (!email.matches()) {
return false;
}
if (!Pattern.matches(USER_PATTERN, email.group(1))) {
return false;
}
String domain = email.group(2);
Matcher ips = Pattern.compile(IP_DOMAIN_PATTERN).matcher(domain);
if (ips.matches()) {
for (int i = 1; i <= 4; i++) {
if (Integer.parseInt(ips.group(i)) > 255) {
return false;
}
}
return true;
}
if (!Pattern.matches(DOMAIN_PATTERN, domain)) {
return false;
}
return true;
}
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("[email protected]");
list.add("te%$#[email protected]");
list.add("[email protected]");
list.add("[email protected]");
list.add("te我的家@test.com [email protected]");
list.add("Alfred.Neuman@BBN-TENEXA");
list.add("\"George,Ted\"@Group.Arpanet");
list.add("Wilt(the Stilt)[email protected] [email protected]");
for (String str : list) {
System.out.println(isValid(str));
}
System.out.println();
// 从输入字符串中提取电子邮件地址
Pattern ascill = Pattern.compile(LEGAL_ASCII_PATTERN);
Pattern p = Pattern.compile(SINGLE_EMAIL_PATTERN);
Matcher m = p.matcher("");
String temp = null;
for (String str : list) {
m.reset(str);
while (m.find()) {
temp = m.group();
if (ascill.matcher(temp).matches()) {
System.out.println(temp);
}
}
}
}
运行结果:
true
true
true
false
false
true
true
false
[email protected]
te % $#[email protected]
test@ 127.0 . 0.1
test@ 127.258 . 0.1
[email protected]
Alfred.Neuman@BBN - TENEXA
[email protected]
[email protected]
true
true
false
false
true
true
false
[email protected]
te % $#[email protected]
test@ 127.0 . 0.1
test@ 127.258 . 0.1
[email protected]
Alfred.Neuman@BBN - TENEXA
[email protected]
[email protected]