public class VerifyEmailUtil {
private static Logger logger = LoggerFactory.getLogger(VerifyEmailUtil.class);
private static final String EMAIL_REGEX = new StringBuffer().append("^([a-zA-Z0-9_.+-]").append("|").append("[")
.append("`").append("~").append("!").append("#").append("$").append("%").append("^").append("&").append("*").append("=")
.append(",").append(".").append("<").append(">").append(";").append(":").append("'").append("\"").append("")
.append("\\[").append("\\]").append("\\(").append("\\)").append("\\{").append("\\}").append("\\|").append("\\").append("\\/")
.append("]").append(")+").append("@(([a-zA-Z0-9-])+.)+").append("([a-zA-Z0-9]{2,4})+$").toString();
//"^([a-zA-Z0-9_.+-]|[*`~!#%&=,.<>;:'"?$^\[\]\(\)\{\}\|\\\/])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$";
private static final String EMAIL_DOMAIN_REGEX = "@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
private static Record[] records = null;
private static List<String> domainWhiteList = null;
static {
domainWhiteList = Arrays.asList(ConfigProperties.getInstance().getValue("domain_whitelist").split(","));
}
/**
* 语法检验
* @param email
* @return
*/
public static boolean isValidFormat(String email) {
if (StringUtils.isEmpty(email)) {
return false;
}
if (Pattern.matches(EMAIL_REGEX, email)) {
// 语法校验通过
return true;
} else {
// 语法校验未通过
return false;
}
}
/**
* 域名语法校验
* @param domain
* @return
*/
public static boolean isValidDomainFormat(String domain) {
if (StringUtils.isEmpty(domain)) {
return false;
}
if (Pattern.matches(EMAIL_DOMAIN_REGEX, domain)) {
// 语法校验通过
return true;
} else {
// 语法校验未通过
return false;
}
}
/**
* 是否存在白名单种
* @param domain
* @return
*/
public static boolean isInDomainWhiteList(String domain) {
if (domainWhiteList.contains(domain)) {
return true;// 存在域名白名单中,直接跳过
}
return false;
}
/**
* MX记录分析
* @param email
* @return
*/
public static boolean isValidDomain(String email) {
String domain = email.split(StringConstant.AT)[1].toLowerCase();
Lookup lookup = null;
try {
lookup = new Lookup(domain, Type.MX);
} catch (TextParseException e) {
logger.error(e.getMessage(), e);
}
lookup.run();
if (lookup.getResult() != Lookup.SUCCESSFUL) {
// 找不到MX记录
logger.info(domain + "'s mx not exists.");
return false;
} else {
// MX记录存在
logger.info(domain + "'s mx exists.");
records = lookup.getAnswers();
/*if (!ArrayUtils.isEmpty(records)) {
for (Record record : records) {
logger.info(email + "'s MX record: " + record.getAdditionalName().toString());
}
}*/
return true;
}
}
/**
* SMTP
* @param email
* @return
*/
public static boolean isValidUser(String email) {
final String SENDER_EMAIL = "no-reply@qq.com";
final String SENDER_EMAIL_DOMAIN = SENDER_EMAIL.split(StringConstant.AT)[1];
if (!ArrayUtils.isEmpty(records)) {
int count = 0;// 计数器
SMTPClient client = new SMTPClient();
for (Record record : records) {
String hostname = record.getAdditionalName().toString();
try {
client.connect(hostname);// 连接到接收邮箱地址的邮箱服务器
} catch (Exception e) {
count++;
if (count >= records.length) {
// 如果由MX得到的result服务器都连接不上,则判定email无效
return false;
}
}
if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
// 服务器通信失败
try {
client.disconnect();
continue;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
} else {
logger.info("MX record about " + hostname + "exists. ");
logger.info("Connection succeeded to " + hostname + " SMTP.");
logger.info(client.getReplyString());
try {
client.login(SENDER_EMAIL_DOMAIN);
logger.info("> HELO " + hostname);
logger.info(client.getReplyString());
client.setSender(SENDER_EMAIL);
if (client.getReplyCode() != 250) {
client.disconnect();
continue;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.info("> MAIL FROM: <" + SENDER_EMAIL + ">");
logger.info(client.getReplyString());
try {
client.disconnect();
continue;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
logger.info("> RCPT TO: <" + email + ">");
logger.info(client.getReplyString());
if (client.getReplyCode() == 250) {
// SMTP通讯成功
return true;
}
try {
client.disconnect();
continue;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
} else {
// SMTP失败
return false;
}
return false;
}
public static void main(String[] args) {
System.out.println(isValidFormat("*********@qq.com"));
System.out.println(isValidFormat("$---@qq.com"));
System.out.println(isValidFormat("#*****$@qq.com"));
System.out.println(isValidFormat("!123******@qq.com"));
System.out.println(isValidDomain("@qq.com"));
System.out.println(isValidDomainFormat("@aapindustries.com.au"));
}
}