邮件 有效性验证 email

package com;

import org.apache.commons.net.smtp.SMTPClient;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Type;

public class TestMail3 {

	public static String email1 = "[email protected]";
	public static String email2 = "[email protected]";
	public static String email3 = "[email protected]";
	public static String email4 = "[email protected]";
	public static String email5 = "[email protected]";
	public static String email6 = "[email protected]";
	public static String email7 = "[email protected]";
	public static String email8 = "[email protected]";
	public static String email9 = "[email protected]";
	public static String email10 = "[email protected]";

	static SMTPClient client;
	static Lookup lookup;
	static {
		try {
			client = new SMTPClient();
			lookup = new Lookup("hp.com", Type.MX);
			lookup.run();
			client.connect("smtp3.hp.com");
			client.setSender("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {

		long begin = System.currentTimeMillis(); // 测试起始时间

		System.out.println(email1 + "-->" + checkEmail(email1));
		System.out.println(email2 + "-->" + checkEmail(email2));
		System.out.println(email3 + "-->" + checkEmail(email3));
		System.out.println(email4 + "-->" + checkEmail(email4));
		System.out.println(email5 + "-->" + checkEmail(email5));
		System.out.println(email6 + "-->" + checkEmail(email6));
		System.out.println(email7 + "-->" + checkEmail(email7));
		System.out.println(email8 + "-->" + checkEmail(email8));
		System.out.println(email9 + "-->" + checkEmail(email9));
		System.out.println(email10 + "-->" + checkEmail(email10));
		
		long end = System.currentTimeMillis(); // 测试结束时间  
        System.out.println("操作所需时间:" + (end - begin));
	}

	public static boolean checkEmail(String email) throws Exception {
		if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
			System.out.println("regx failed");
			return false;
		}

		client.addRecipient(email);
		if (250 == client.getReplyCode()) {
			return true;
		}
		return false;
	}

}

你可能感兴趣的:(email)