Guest User

Untitled

a guest
Feb 19th, 2018
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.38 KB | None | 0 0
  1. public class EmailTest {
  2. private static int hear(BufferedReader in) throws IOException {
  3. String line = null;
  4. int res = 0;
  5.  
  6. while ((line = in.readLine()) != null) {
  7. String pfx = line.substring(0, 3);
  8. try {
  9. res = Integer.parseInt(pfx);
  10. } catch (Exception ex) {
  11. res = -1;
  12. }
  13. if (line.charAt(3) != '-')
  14. break;
  15. }
  16.  
  17. return res;
  18. }
  19.  
  20. private static void say(BufferedWriter wr, String text) throws IOException {
  21. wr.write(text + "rn");
  22. wr.flush();
  23.  
  24. return;
  25. }
  26.  
  27. @SuppressWarnings({ "rawtypes", "unchecked" })
  28. private static ArrayList getMX(String hostName) throws NamingException {
  29. // Perform a DNS lookup for MX records in the domain
  30. Hashtable env = new Hashtable();
  31. env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
  32. DirContext ictx = new InitialDirContext(env);
  33. Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
  34. Attribute attr = attrs.get("MX");
  35.  
  36. // if we don't have an MX record, try the machine itself
  37. if ((attr == null) || (attr.size() == 0)) {
  38. attrs = ictx.getAttributes(hostName, new String[] { "A" });
  39. attr = attrs.get("A");
  40. if (attr == null)
  41. throw new NamingException("No match for name '" + hostName + "'");
  42. }
  43. /*
  44. Huzzah! we have machines to try. Return them as an array list
  45. NOTE: We SHOULD take the preference into account to be absolutely
  46. correct. This is left as an exercise for anyone who cares.
  47. */
  48. ArrayList res = new ArrayList();
  49. NamingEnumeration en = attr.getAll();
  50.  
  51. while (en.hasMore()) {
  52. String mailhost;
  53. String x = (String) en.next();
  54. String f[] = x.split(" ");
  55. // THE fix *************
  56. if (f.length == 1)
  57. mailhost = f[0];
  58. else if (f[1].endsWith("."))
  59. mailhost = f[1].substring(0, (f[1].length() - 1));
  60. else
  61. mailhost = f[1];
  62. // THE fix *************
  63. res.add(mailhost);
  64. }
  65. return res;
  66. }
  67.  
  68. @SuppressWarnings("rawtypes")
  69. public static boolean isAddressValid(String address) {
  70. // Find the separator for the domain name
  71. int pos = address.indexOf('@');
  72.  
  73. // If the address does not contain an '@', it's not valid
  74. if (pos == -1)
  75. return false;
  76.  
  77. // Isolate the domain/machine name and get a list of mail exchangers
  78. String domain = address.substring(++pos);
  79. ArrayList mxList = null;
  80. try {
  81. mxList = getMX(domain);
  82. } catch (NamingException ex) {
  83. return false;
  84. }
  85.  
  86. /*
  87. Just because we can send mail to the domain, doesn't mean that the
  88. address is valid, but if we can't, it's a sure sign that it isn't
  89. */
  90. if (mxList.size() == 0)
  91. return false;
  92.  
  93. /*
  94. Now, do the SMTP validation, try each mail exchanger until we get
  95. a positive acceptance. It *MAY* be possible for one MX to allow
  96. a message [store and forwarder for example] and another [like
  97. the actual mail server] to reject it. This is why we REALLY ought
  98. to take the preference into account.
  99. */
  100. for (int mx = 0; mx < mxList.size(); mx++) {
  101. boolean valid = false;
  102. try {
  103. int res;
  104. //
  105. Socket skt = new Socket((String) mxList.get(mx), 25);
  106. BufferedReader rdr = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  107. BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
  108.  
  109. res = hear(rdr);
  110. if (res != 220)
  111. throw new Exception("Invalid header");
  112. say(wtr, "EHLO rgagnon.com");
  113.  
  114. res = hear(rdr);
  115. if (res != 250)
  116. throw new Exception("Not ESMTP");
  117.  
  118. // validate the sender address
  119. say(wtr, "MAIL FROM: <[email protected]>");
  120. res = hear(rdr);
  121. if (res != 250)
  122. throw new Exception("Sender rejected");
  123.  
  124. say(wtr, "RCPT TO: <" + address + ">");
  125. res = hear(rdr);
  126.  
  127. // be polite
  128. say(wtr, "RSET");
  129. hear(rdr);
  130. say(wtr, "QUIT");
  131. hear(rdr);
  132. if (res != 250)
  133. throw new Exception("Address is not valid!");
  134.  
  135. valid = true;
  136. rdr.close();
  137. wtr.close();
  138. skt.close();
  139. } catch (Exception ex) {
  140. // Do nothing but try next host
  141. ex.printStackTrace();
  142. } finally {
  143. if (valid)
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149.  
  150. public static void main(String args[]) {
  151. System.out.println(testData.length);
  152. for (int ctr = 0; ctr < testData.length; ctr++) {
  153. System.out.println(testData[ctr] + " is valid? " + isAddressValid(testData[ctr]));
  154. }
  155. return;
  156. }
  157. }
  158.  
  159. public class EmailTest {
  160. private static int hear(BufferedReader in) throws IOException {
  161. String line = null;
  162. int res = 0;
  163.  
  164. while ((line = in.readLine()) != null) {
  165. String pfx = line.substring(0, 3);
  166. try {
  167. res = Integer.parseInt(pfx);
  168. } catch (Exception ex) {
  169. res = -1;
  170. }
  171. if (line.charAt(3) != '-')
  172. break;
  173. }
  174.  
  175. return res;
  176. }
  177.  
  178. private static void say(BufferedWriter wr, String text) throws IOException {
  179. wr.write(text + "rn");
  180. wr.flush();
  181.  
  182. return;
  183. }
  184.  
  185. @SuppressWarnings({ "rawtypes", "unchecked" })
  186. private static ArrayList getMX(String hostName) throws NamingException {
  187. // Perform a DNS lookup for MX records in the domain
  188. Hashtable env = new Hashtable();
  189. env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
  190. DirContext ictx = new InitialDirContext(env);
  191. Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
  192. Attribute attr = attrs.get("MX");
  193.  
  194. // if we don't have an MX record, try the machine itself
  195. if ((attr == null) || (attr.size() == 0)) {
  196. attrs = ictx.getAttributes(hostName, new String[] { "A" });
  197. attr = attrs.get("A");
  198. if (attr == null)
  199. throw new NamingException("No match for name '" + hostName + "'");
  200. }
  201. /*
  202. Huzzah! we have machines to try. Return them as an array list
  203. NOTE: We SHOULD take the preference into account to be absolutely
  204. correct. This is left as an exercise for anyone who cares.
  205. */
  206. ArrayList res = new ArrayList();
  207. NamingEnumeration en = attr.getAll();
  208.  
  209. while (en.hasMore()) {
  210. String mailhost;
  211. String x = (String) en.next();
  212. String f[] = x.split(" ");
  213. // THE fix *************
  214. if (f.length == 1)
  215. mailhost = f[0];
  216. else if (f[1].endsWith("."))
  217. mailhost = f[1].substring(0, (f[1].length() - 1));
  218. else
  219. mailhost = f[1];
  220. // THE fix *************
  221. res.add(mailhost);
  222. }
  223. return res;
  224. }
  225.  
  226. @SuppressWarnings("rawtypes")
  227. public static boolean isAddressValid(String address) {
  228. // Find the separator for the domain name
  229. int pos = address.indexOf('@');
  230.  
  231. // If the address does not contain an '@', it's not valid
  232. if (pos == -1)
  233. return false;
  234.  
  235. // Isolate the domain/machine name and get a list of mail exchangers
  236. String domain = address.substring(++pos);
  237. ArrayList mxList = null;
  238. try {
  239. mxList = getMX(domain);
  240. } catch (NamingException ex) {
  241. return false;
  242. }
  243.  
  244. /*
  245. Just because we can send mail to the domain, doesn't mean that the
  246. address is valid, but if we can't, it's a sure sign that it isn't
  247. */
  248. if (mxList.size() == 0)
  249. return false;
  250.  
  251. /*
  252. Now, do the SMTP validation, try each mail exchanger until we get
  253. a positive acceptance. It *MAY* be possible for one MX to allow
  254. a message [store and forwarder for example] and another [like
  255. the actual mail server] to reject it. This is why we REALLY ought
  256. to take the preference into account.
  257. */
  258. for (int mx = 0; mx < mxList.size(); mx++) {
  259. boolean valid = false;
  260. try {
  261. int res;
  262. //
  263. Socket skt = new Socket((String) mxList.get(mx), 25);
  264. BufferedReader rdr = new BufferedReader(new InputStreamReader(skt.getInputStream()));
  265. BufferedWriter wtr = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream()));
  266.  
  267. res = hear(rdr);
  268. if (res != 220)
  269. throw new Exception("Invalid header");
  270. say(wtr, "EHLO rgagnon.com");
  271.  
  272. res = hear(rdr);
  273. if (res != 250)
  274. throw new Exception("Not ESMTP");
  275.  
  276. // validate the sender address
  277. say(wtr, "MAIL FROM: <[email protected]>");
  278. res = hear(rdr);
  279. if (res != 250)
  280. throw new Exception("Sender rejected");
  281.  
  282. say(wtr, "RCPT TO: <" + address + ">");
  283. res = hear(rdr);
  284.  
  285. // be polite
  286. say(wtr, "RSET");
  287. hear(rdr);
  288. say(wtr, "QUIT");
  289. hear(rdr);
  290. if (res != 250)
  291. throw new Exception("Address is not valid!");
  292.  
  293. valid = true;
  294. rdr.close();
  295. wtr.close();
  296. skt.close();
  297. } catch (Exception ex) {
  298. // Do nothing but try next host
  299. ex.printStackTrace();
  300. } finally {
  301. if (valid)
  302. return true;
  303. }
  304. }
  305. return false;
  306. }
  307.  
  308. public static void main(String args[]) {
  309. System.out.println(testData.length);
  310. for (int ctr = 0; ctr < testData.length; ctr++) {
  311. System.out.println(testData[ctr] + " is valid? " + isAddressValid(testData[ctr]));
  312. }
  313. return;
  314. }
  315. }
  316.  
  317. // Create a new instance of the EmailValidator class.
  318. EmailValidator em = new EmailValidator();
  319. em.MessageLogging += em_MessageLogging;
  320. em.EmailValidated += em_EmailValidationCompleted;
  321. try
  322. {
  323. string[] list = new string[3] { "[email protected]", "[email protected]", "[email protected]" };
  324. em.ValidateEmails(list);
  325. }
  326. catch (EmailValidatorException exc2)
  327. {
  328. Console.WriteLine("EmailValidatorException: " + exc2.Message);
  329. }
Add Comment
Please, Sign In to add comment