日期:2014-05-20 浏览次数:20929 次
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; /** * PingMailDomain.java * @author jileniao.net * 判断输入的邮件地址@后的域名是否正确 */ public class PingMailDomain { private Boolean isMailDomailRight(String emailAdd) throws IOException { String[] tmp = emailAdd.split("@"); // 简单校验邮件地址是否含有@号:非有效邮箱地址 if (!emailAdd.contains("@")) { return false; } String domain = tmp[1]; String cmdString = "cmd /c ping " + domain; Process prs = Runtime.getRuntime().exec(cmdString); BufferedReader br = new BufferedReader(new InputStreamReader(prs.getInputStream())); String line = null; StringBuffer sb = new StringBuffer(); // 得到ping结果信息 while ((line = br.readLine()) != null) { sb.append(line); } String result = sb.toString(); String[] temp = result.split("%"); String result1 = temp[0]; // ping结果loss为100%:非有效邮箱域名 if (result1.endsWith("100")) { return false; } return true; } public static void main(String[] args) throws Exception { System.out.println("Please input EmailAddress"); Scanner input = new Scanner(System.in); // 获取键盘输入的邮件地址 String addressString = input.nextLine(); PingMailDomain pmd = new PingMailDomain(); if (pmd.isMailDomailRight(addressString)) { System.out.println("OK"); } else { System.out.println("NG"); } } }