2

我正在尝试验证电子邮件 ID 是否真实存在。在做了一些 RND 之后,能够找到一个代码来检查电子邮件 ID 的存在,并且我通过 MX 记录来执行此操作。它适用于本地服务器,但我无法使用套接字连接到某些标准邮件服务器。我正在尝试通过端口号连接。25但每次都失败。任何人都应该告诉我有什么问题吗?您的帮助将不胜感激。下面是类文件。

    package com.wm.restful.webservices.client;

    import java.io.BufferedReader;  
    import java.io.BufferedWriter;  
    import java.io.IOException;  
    import java.io.InputStreamReader;  
    import java.io.OutputStreamWriter;  
    import java.net.Socket;  
    import java.util.ArrayList;  
    import java.util.Hashtable;  
    import javax.naming.NamingEnumeration;  
    import javax.naming.NamingException;  
    import javax.naming.directory.Attribute;  
    import javax.naming.directory.Attributes;  
    import javax.naming.directory.DirContext;  
    import javax.naming.directory.InitialDirContext;

    public class SMTPMXLookup {
       private static int hear( BufferedReader in ) throws IOException {  
         String line = null;  
         int res = 0;  
         while ( (line = in.readLine()) != null ) {  
             String pfx = line.substring( 0, 3 );  
             try {  
                res = Integer.parseInt( pfx );  
             }  
             catch (Exception ex) {  
                res = -1;  
             }  
             if ( line.charAt( 3 ) != '-' ) break;  
         }  
         return res;  
         }

       private static void say( BufferedWriter wr, String text )  
          throws IOException {  
         wr.write( text + "\r\n" );  
         wr.flush();  

         return;  
         }  
         private static ArrayList getMX( String hostName )  
             throws NamingException {  

         Hashtable env = new Hashtable();  
         env.put("java.naming.factory.initial",  
                 "com.sun.jndi.dns.DnsContextFactory");  
         DirContext ictx = new InitialDirContext( env );  
         Attributes attrs = ictx.getAttributes  
                               ( hostName, new String[] { "MX" });  
         Attribute attr = attrs.get( "MX" );  

           if (( attr == null ) || ( attr.size() == 0 )) {
           attrs = ictx.getAttributes( hostName, new String[] { "A" });
           attr = attrs.get( "A" );
           if( attr == null )
                throw new NamingException
                         ( "No match for name '" + hostName + "'" );
         }

         ArrayList res = new ArrayList();
         NamingEnumeration en = attr.getAll();

         while ( en.hasMore() ) {
            String mailhost;
            String x = (String) en.next();
            String f[] = x.split( " " );

            if (f.length == 1)
                mailhost = f[0];
            else if ( f[1].endsWith( "." ) )
                mailhost = f[1].substring( 0, (f[1].length() - 1));
            else
                mailhost = f[1];

            res.add( mailhost );
         }
         return res;
         }
       public static boolean isAddressValid( String address ) {     
         int pos = address.indexOf( '@' );     
         if ( pos == -1 ) return false;     
         String domain = address.substring( ++pos );  
         ArrayList mxList = null;  
         try {  
            mxList = getMX( domain );  
         }  
         catch (NamingException ex) {  
            return false;  
         }  

         if ( mxList.size() == 0 ) return false;

         for ( int mx = 0 ; mx < mxList.size() ; mx++ ) {
             boolean valid = false;
             try {
                 int res;

                 Socket skt = new Socket( (String) mxList.get( mx ), 995 );
                 BufferedReader rdr = new BufferedReader
                    ( new InputStreamReader( skt.getInputStream() ) );
                 BufferedWriter wtr = new BufferedWriter
                    ( new OutputStreamWriter( skt.getOutputStream() ) );

                 res = hear( rdr );
                 if ( res != 220 ) throw new Exception( "Invalid header" );
                 say( wtr, "EHLO rgagnon.com" );

                 res = hear( rdr );
                 if ( res != 250 ) throw new Exception( "Not ESMTP" );


                 say( wtr, "MAIL FROM: <tim@orbaker.com>" );
                 res = hear( rdr );
                 if ( res != 250 ) throw new Exception( "Sender rejected" );

                 say( wtr, "RCPT TO: <" + address + ">" );
                 res = hear( rdr );

                 say( wtr, "RSET" ); hear( rdr );
                 say( wtr, "QUIT" ); hear( rdr );
                 if ( res != 250 )
                    throw new Exception("Address is not valid!");

                 valid = true;
                 rdr.close();
                 wtr.close();
                 skt.close();
             }
             catch (Exception ex) {
               ex.printStackTrace();
             }
             finally {
               if ( valid ) return true;
             }
         }
         return false;
         }

       public static void main( String args[] ) {  
         String testData[] = {  
             "xyz@gmail.com", //take an example 
             };  

         for ( int ctr = 0 ; ctr < testData.length ; ctr++ ) {
            System.out.println( testData[ ctr ] + " is valid? " +
                  isAddressValid( testData[ ctr ] ) );
         }
         return;
         }
    }

我能够连接到在 25 号端口上运行的邮件服务器,但是当我尝试使用 gmail.com、google.com、yahoo.com 等的 MX 记录连接到一些标准邮件服务器时,无法连接连接到他们的服务器。非常感谢您的帮助..

4

0 回答 0