/h819-commons/src/main/java/org/h819/commons/MyEmailUtils.java

https://github.com/h819/spring-boot · Java · 125 lines · 59 code · 26 blank · 40 comment · 7 complexity · b52828123a2d01c5e2a34080eb121ee1 MD5 · raw file

  1. package org.h819.commons;
  2. /**
  3. * Description : TODO()
  4. * User: h819
  5. * Date: 2015/3/23
  6. * Time: 17:39
  7. * To change this template use File | Settings | File Templates.
  8. */
  9. import org.apache.commons.net.smtp.SMTPClient;
  10. import org.apache.commons.net.smtp.SMTPReply;
  11. import org.xbill.DNS.Lookup;
  12. import org.xbill.DNS.Record;
  13. import org.xbill.DNS.Type;
  14. import java.io.IOException;
  15. public class MyEmailUtils {
  16. /**
  17. * 利用 dnsjava 和 commons.net ,验证 email 地址是否合法
  18. *
  19. * @param emailAddress
  20. * @return
  21. */
  22. public static boolean validateEmailAddress(String emailAddress) {
  23. if (!emailAddress.matches(RegexConstantParams.reg_email_string)) {
  24. System.err.println("Format error");
  25. return false;
  26. }
  27. String log = "";
  28. String host = "";
  29. String hostName = emailAddress.split("@")[1];
  30. Record[] result = null;
  31. SMTPClient client = new SMTPClient();
  32. boolean mxServerAvilabe = false;
  33. try {
  34. // 根据域名,查找 MX 记录,是否存在。
  35. Lookup lookup = new Lookup(hostName, Type.MX);
  36. lookup.run();
  37. // for (Record r : lookup.getAnswers())
  38. // System.out.println(r);
  39. if (lookup.getResult() != Lookup.SUCCESSFUL) {
  40. log += "找不到MX记录\n";
  41. return false;
  42. } else {
  43. result = lookup.getAnswers();
  44. }
  45. // 连接到邮箱服务器,用 SMTPClient 探测 MX 的域名,是否能访问。
  46. for (int i = 0; i < result.length; i++) {
  47. host = result[i].getAdditionalName().toString();
  48. client.connect(host);
  49. if (!SMTPReply.isPositiveCompletion(client.getReplyCode())) {
  50. client.disconnect();
  51. log += "SMTP server " + host + " refused connection.";
  52. continue;
  53. } else {
  54. log += "MX record about " + hostName + " exists.\n";
  55. log += "Connection succeeded to " + host + "\n";
  56. mxServerAvilabe = true;
  57. break;
  58. }
  59. }
  60. return mxServerAvilabe;
  61. // 下面代码是模拟登陆,但 outlook.com 邮箱不允许本机登陆,所以下面代码不能运行。
  62. // log += client.getReplyString();
  63. // // HELO cyou-inc.com
  64. // client.login("cyou-inc.com");
  65. // log += ">HELO cyou-inc.com\n";
  66. // log += "=" + client.getReplyString();
  67. //
  68. // // MAIL FROM: <zhaojinglun@cyou-inc.com>
  69. // client.setSender("zhaojinglun@cyou-inc.com");
  70. // log += ">MAIL FROM: <zhaojinglun@cyou-inc.com>\n";
  71. // log += "=" + client.getReplyString();
  72. //
  73. // // RCPT TO: <$emailAddress>
  74. // client.addRecipient(emailAddress);
  75. // log += ">RCPT TO: <" + emailAddress + ">\n";
  76. // log += "=" + client.getReplyString();
  77. //
  78. // if (250 == client.getReplyCode()) {
  79. // return true;
  80. // }
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. } finally {
  84. try {
  85. client.disconnect();
  86. } catch (IOException e) {
  87. }
  88. // 打印日志
  89. System.err.println(log);
  90. }
  91. return false;
  92. }
  93. public static void main(String[] args) {
  94. // System.err.println("Outcome: "
  95. // + MyEmailUtils.validateEmailAddress("1729866861@qq.com"));
  96. System.err.println("Outcome: "
  97. + MyEmailUtils.validateEmailAddress("h81900@outlook.com")); //
  98. }
  99. }