问题描述
下面是我的代码,问题出的不知道什么原因,不知道是不是系统区别问题,在windows下面运行是可以正常检查出来,但是在Linux下面就不行了,求解答 package string.code;import java.io.IOException;import org.apache.commons.net.smtp.SMTPClient;import org.apache.commons.net.smtp.SMTPReply;import org.xbill.DNS.Lookup;import org.xbill.DNS.Record;import org.xbill.DNS.Type;public class CheckEmailObj {public static boolean checkEmail(String email) {DealString ds = new DealString();// if (!email.matches("[\w\.\-]+@([\w\-]+\.)+[\w\-]+")) {if (ds.emailFormat(email) == false) {// System.err.println("Format error");return false;}getProperties getset = new getProperties();String efrom = getset.readValue("gecxml/config.properties", "mail.from");String[] efromarray = efrom.split("@");String log = "";String host = "";String hostName = email.split("@")[1];Record[] result = null;SMTPClient client = new SMTPClient();try {System.out.println(hostName + " hostName");System.out.println(Type.MX + " Type.MX");// 查找MX记录Lookup lookup = new Lookup(hostName, Type.MX);System.out.println(lookup + " lookup");lookup.run();System.out.println(lookup.getAnswers() + " lookup.getAnswers()");System.out.println(lookup.getResult() + " lookup.getResult()");System.out.println(Lookup.SUCCESSFUL + " Lookup.SUCCESSFUL");if (lookup.getResult() != Lookup.SUCCESSFUL) {log += "找不到MX记录n";return false;} else {result = lookup.getAnswers();}// 连接到邮箱服务器for (int i = 0; i < result.length; i++) {host = result[i].getAdditionalName().toString();client.connect(host);if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {client.disconnect();continue;} else {log += "MX record about " + hostName + " exists.n";log += "Connection succeeded to " + host + "n";break;}}log += client.getReplyString();// HELO cyou-inc.comclient.login(efromarray[0].toString());log += ">HELO localhostn";log += "=" + client.getReplyString();// MAIL FROM: <zhaojinglun@cyou-inc.com>client.setSender("info@localhost.org");log += ">MAIL FROM: <info@localhost.org>n";log += "=" + client.getReplyString();// RCPT TO: <$email>client.addRecipient(email);log += ">RCPT TO: <" + email + ">n";log += "=" + client.getReplyString();if (250 == client.getReplyCode()) {return true;}} catch (Exception e) {e.printStackTrace();} finally {try {client.disconnect();} catch (IOException e) {}// 打印日志System.err.println(log);}return false;} 问题补充:不是编码问题,所以值都是正常的,我换了一个发送去试邮件发送失败! javax.mail.MessagingException: Unknown SMTP host:smtp.163.com;nested exception is:java.net.UnknownHostException: smtp.163.com出的这个错误,但是机器可以连接外网,头晕中
解决方案
既然不是程序问题,那有可能就是linux下的host映射,访问权限等等问题,这个你不要通过程序来找问题,通过你的机器来找问题,首先能上网是不行的,试试能不能ping通stmp.163.com再说。
解决方案二:
1.检查一下你的properties文件编码问题,是否已经乱码,window和linux下编码不同,乱码的可能性很大。2.建议像这种问题,用debug调试一下,什么问题都解决了。