MAVEN
<! - Verify the mailbox is available -> <dependency> <groupId> dnsjava </ groupId> <artifactId> dnsjava </ artifactId> <Version> 2.1.1 </ Version> </ dependency>
JAVA code
package com.jm.mail.tools; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.net.Socket; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.lang.ArrayUtils; importorg.apache.commons.lang.StringUtils; Import org.apache.commons.lang.builder.CompareToBuilder; Import org.xbill.DNS.Lookup; Import org.xbill.DNS.MXRecord; Import org.xbill.DNS.Record; Import org.xbill.DNS.TextParseException; Import org.xbill.DNS.Type; public class MailValid { public static void main (String [] args) { System.out.println ( new new . MailValid () Valid ( "a111aaaaaaaaaaa @ 163 .com "," jootmir.org " )); } / ** * verify mailbox exists * <br> * to read the IO, will cause the thread to block * * @Param toMail * To verify that the mailbox * @param Domain * issuing domain name verification request (the current site of the domain name, you can specify any) * @return * mailbox is up * / public static boolean ! Valid (toMail String, String Domain) { IF (StringUtils.isBlank (toMail) || StringUtils.isBlank (Domain)) return to false ; IF (! StringUtils.contains (toMail, '@')) return to false ; String Host = toMail.substring (toMail.indexOf ( '@') +. 1 ); IF(host.equals (Domain)) return to false ; the Socket Socket = new new the Socket (); the try { // find mx records the Record [] = MXrecords new new the Lookup (Host, Type.MX) .run (); IF (ArrayUtils.isEmpty (MXrecords)) return to false ; // mail server address String mxHost = ((The MXRecord) MXrecords [0 ]) the getTarget () toString ();.. IF (mxRecords.length>. 1) { // prioritizing List <Record > = arrRecords new new the ArrayList <the Record> ( ); Collections.addAll(arrRecords, mxRecords); Collections.sort(arrRecords, new Comparator<Record>() { public int compare(Record o1, Record o2) { return new CompareToBuilder().append(((MXRecord)o1).getPriority(), ((MXRecord)o2).getPriority()).toComparison(); } }); mxHost = ((MXRecord)arrRecords.get(0)).getTarget().toString(); } // 开始smtp socket.connect(new InetSocketAddress(mxHost, 25)); BufferedReader the BufferedReader = new new the BufferedReader ( new new the InputStreamReader ( new new BufferedInputStream (Socket.getInputStream ()))); BufferedWriter, BufferedWriter = new new BufferedWriter, ( new new the OutputStreamWriter (Socket.getOutputStream ())); // timeout (ms) Long timeout = 6000 ; // sleep time segment (50 ms) int sleepSect = 50 ; // connection (the server is ready) iF (getResponseCode (timeout, sleepSect, BufferedReader) 220 =! {) return to false ; } // 握手 bufferedWriter.write("HELO " + domain + "\r\n"); bufferedWriter.flush(); if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) { return false; } // 身份 bufferedWriter.write("MAIL FROM: <check@" + domain + ">\r\n"); bufferedWriter.flush(); if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) { return false; } // 验证 bufferedWriter.write("RCPT TO: <" + toMail + ">\r\n"); bufferedWriter.flush(); if(getResponseCode(timeout, sleepSect, bufferedReader) != 250) { return false; } // 断开 bufferedWriter.write("QUIT\r\n"); bufferedWriter.flush(); return true; } catch (NumberFormatException e) { e.printStackTrace(); } catch (TextParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { } } return false; } private static int getResponseCode(long timeout, int sleepSect, BufferedReader bufferedReader) throws InterruptedException, NumberFormatException, IOException { int code = 0; for(long i = sleepSect; i < timeout; i += sleepSect) { Thread.sleep(sleepSect); if(bufferedReader.ready()) { String outline = bufferedReader.readLine(); // FIXME 读完…… while(bufferedReader.ready()) /*System.out.println(*/bufferedReader.readLine()/*)*/; /*System.out.println(outline);*/ code = Integer.parseInt(outline.substring(0, 3)); break; } } return code; } }