如何准确判断邮件地址是否存在
我总结了几种邮件出现重发、漏发的解释:1.网络;2.防火墙;3.服务器的自我保护,比如防止大批量发送时挂掉或者垃圾邮件,我觉得第三种解释靠谱一些,对于遇到的这些问题在下面的文章中给出了补救措施。
公司邮箱目前使用的是zimbra,该邮件服务器目前不甚稳定,经常出现重发、漏发问题。经测试,每100封邮件仅可成功发送98封左右,以下是测试数据:
测试用例1:100封,总用时约:16min;实收97封,失败3次,3次错误信息均为:javax.mail.messagingexception: could not connect to smtp host
测试用例2:100封,总用时约:16min;实收100封,失败2次,错误同上。加失败重发机制,失败后等待10s重发,最多重发3次;
测试用例3:每发一封,停留10s,总用时32min;实收100封,失败1次,错误同上;重发机制同用例2.
关于messagingexception的问题,可以参考:
javax.mail.messagingexception: could not connect to smtp host
针对这种问题,我增加了邮件重发,
if(sendhtmlmail_(mail)){ return true; } else{ int i = 0; //包含群组邮件,失败不重发 boolean isneedre = isneedre(mail); while(!sendhtmlmail_(mail) && isneedre && i < 10){ try { i++; thread.sleep(1000*60); } catch (interruptedexception e) { logger.error("resend mail error", e); } } return true; }
但这种机制又产生了新的问题,因邮件服务器不稳定导致在仅发送一次的情况下也会向邮件收件人发送邮件,且同一封邮件的收件人(包括抄送、密送)可能部分收到邮件、部分收不到邮件。
针对以上的问题,我们将重发机制去除,仅针对不合法邮件(即服务器上不存在的邮件地址)进行剔除,剔除后再进行发送。而对其他原因导致的邮件发送失败不做重发(该问题将通过邮件服务器运维部门向厂商反映)。
下面是判断邮件是否合法的逻辑:
1.smtp是工作在两种情况下:一是电子邮件从客户机传输到服务器;二是从某一个服务器传输到另一个服务器
2.smtp是个请求/响应协议,命令和响应都是基于ascii文本,并以cr和lf符结束。响应包括一个表示返回状态的三位数字代码
3.smtp在tcp协议25号端口监听连接请求
4.连接和发送过程
smtp协议说复杂也不复杂,说简单如果你懂得socket。不过现在只是我们利用的就是第一条中说的,从客户机传输到服务器,当我们向一台服务器发送邮件时,邮件服务器会首先验证邮件发送地址是否真的存在于本服务器上。
5 操作的步骤如下:
连接服务器的25端口(如果没有邮件服务,连了也是白连)
发送helo问候
发送mail from命令,如果返回250表示正确可以,连接本服务器,否则则表示服务器需要发送人验证。
发送rcpt to命令,如果返回250表示则email存在
发送quit命令,退出连接
基于上面这个逻辑,我们封装邮件服务器形成socket,发送命令,根据返回值来判断邮件地址是否合法:
具体代码如下:
import java.io.*; import java.net.*; import java.util.*; import javax.naming.*; import javax.naming.directory.*; 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 { // perform a dns lookup for mx records in the domain 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 we don't have an mx record, try the machine itself 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 + "'" ); } // huzzah! we have machines to try. return them as an array list // note: we should take the preference into account to be absolutely // correct. this is left as an exercise for anyone who cares. arraylist res = new arraylist(); namingenumeration en = attr.getall(); while ( en.hasmore() ) { string mailhost; string x = (string) en.next(); string f[] = x.split( " " ); // the fix ************* 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]; // the fix ************* res.add( mailhost ); } return res; } public static boolean isaddressvalid( string address ) { // find the separator for the domain name int pos = address.indexof( '@' ); // if the address does not contain an '@', it's not valid if ( pos == -1 ) return false; // isolate the domain/machine name and get a list of mail exchangers string domain = address.substring( ++pos ); arraylist mxlist = null; try { mxlist = getmx( domain ); } catch (namingexception ex) { return false; } // just because we can send mail to the domain, doesn't mean that the // address is valid, but if we can't, it's a sure sign that it isn't if ( mxlist.size() == 0 ) return false; // now, do the smtp validation, try each mail exchanger until we get // a positive acceptance. it *may* be possible for one mx to allow // a message [store and forwarder for example] and another [like // the actual mail server] to reject it. this is why we really ought // to take the preference into account. for ( int mx = 0 ; mx < mxlist.size() ; mx++ ) { boolean valid = false; try { int res; // socket skt = new socket( (string) mxlist.get( mx ), 25 ); 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" ); // validate the sender address 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 ); // be polite 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) { // do nothing but try next host ex.printstacktrace(); } finally { if ( valid ) return true; } } return false; } public static void main( string args[] ) { string testdata[] = { "real@rgagnon.com", "you@acquisto.net", "fail.me@nowhere.spam", // invalid domain name "arkham@bigmeanogre.net", // invalid address "nosuchaddress@yahoo.com" // failure of this method }; for ( int ctr = 0 ; ctr < testdata.length ; ctr++ ) { system.out.println( testdata[ ctr ] + " is valid? " + isaddressvalid( testdata[ ctr ] ) ); } return; } }
以上是判断邮件地址是否合法的逻辑,如果邮件地址不合法,则将邮件地址从收件人列表中剔除。
private static string[] removeinvalidateaddress(string[] addresses, string mailfrom) { arraylist<string> validateaddresses = new arraylist<string>(); string normaladdress = null; int code; smtptransport smpttrans = null; if(stringutils.isempty(mailfrom) || null == addresses) { return new string[0]; } string sendcmd = "mail from:" + normalizeaddress(mailfrom); try { smpttrans = (smtptransport)sendsession.gettransport("smtp"); smpttrans.connect(); code = smpttrans.simplecommand(sendcmd); if(code != 250 && code != 251) { logger.error("send from invalidate" + mailfrom); } else { for(string address : addresses) { normaladdress = normalizeaddress(address); string cmd = "rcpt to:" + normaladdress; code = smpttrans.simplecommand(cmd); if(code == 250 || code == 251) { validateaddresses.add(address); } } } } catch(messagingexception e) { logger.error("validate mail address error. send from " + mailfrom, e); } string[] result = validateaddresses.toarray(new string[validateaddresses.size()]); return result; } private static string normalizeaddress(string addr) { if ((!addr.startswith("<")) && (!addr.endswith(">"))) return "<" + addr + ">"; else return addr; }
以上是本文的全部内容,希望大家能够理解,对大家有所帮助。
上一篇: jquery
下一篇: Java跨域问题的处理详解