I summarized the explanation of several types of emails: 1. Network; 2. Firewall; 3. Self -protection of the server, such as hanging or spam from a large quantities, I think the third explanation is reliable. Some of these issues are given remedy in the following article.
The company's mailbox is currently using Zimbra. The mail server is currently not very stable, and there are often problems with re -issuing and leakage. After testing, every 100 mails can be successfully sent about 98 sections. The following is the test data:
Test case 1: 100 seal, the total time is about: 16min; 97 seals, 3 failed, 3 error information: javax.mail.MessagingException: Could Not Connect to SMTP HOST
Test case 2: 100 seal, the total time is about 16 minutes; 100 seals were reached, 2 failed, and the errors were the same as above. Add failure mechanism for failure, wait for 10s to be re -issued after failure, up to 3 times;
Test case 3: Each section is issued, stay 10s, and takes 32 minutes in total; 100 seals, once fails, once failed, the error is the same as above;
For the problem of MESSAGINGEXCEPTION, you can refer to:
javax.mail.MessagingException: Could Not Connect to SMTP HOST
In response to this kind of problem, I added the email to re -send it,
if (Sendhtmlmail_ (Mail)) {Return True;} Else {int i = 0; // Contains group emails. Isneedre && i < 10) {try {i ++; thread.sleep (1000*60);} Catch (interruptedException E) {logger.error ("Relying Mail Error", E);}} Return True;} However, this mechanism has caused a new problem. Due to the unstable email server, it will also send mail to the mail recipient when sending only once, and the recipient of the same mail (including copying, secret delivery) It may be partly received by emails and part of the email.
In response to the above problems, we will remove the re -issuing mechanism and remove it only for illegal emails (that is, the mail address that does not exist on the server), and then send it after removing. The email sending failure caused by other reasons will not be re -issued (the problem will report to the manufacturer through the mail server operation and maintenance department).
The following is the logic of determining whether the mail is legal:
1. SMTP is in two cases: first, the email is transmitted from the client to the server; the other is transmitted from a certain server to another server
2. SMTP is a request/response protocol. The commands and response are based on ASCII text and ended in CR and LF symbols. The response includes a three -digit code that represents the return state
3. SMTP At the TCP Agreement No. 25, the mouth monitoring request
4. Connection and sending process
The SMTP protocol is not complicated or complicated, and it is simple if you know the socket. But now it is only what we use in the first article, from the client transmitted to the server. When we send an mail to a server, the mail server will first verify whether the mail sending address really exists on this server.
5 The steps of the operation are as follows:
Port 25 connected to the server (if there is no mail service, even the white connection)
Send Helo Greetings Send the Mail From command. If you return 250, it is correct. Connect to the server, otherwise it means that the server needs to send someone to verify.
Send the RCPT TO command. If the return 250 is represented, the email sends a QUIT command. The connection is based on the logic of the above.
The specific code is as follows:
Import java.io.*; Import java.net.*; Import java.util.*; Import javax.naming.*; Import javax.naming.directory.*; kup {Private Static Int Hear (BufferEdreader in) Throws IOEXCEPTION {string line = null; int res. PFX) ;} Catch (Exception EX) {res = -1;} if (line.charat (3)!) Break;} Return res;} Private Static void say (bufferedWriter WR, String TEXT) thr OWS IOEXCEPTION {WR .write (text + "/r/n"); write, 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"); ES Attrs = ICTX.Getattributes (Hostname, New String [] {"Mx"});; attribute attr = Attrs.get ("mx"); // If we doon'T'T'T'T'T'T has been mx record, try the machine itseelf ((ATTR == NULL) || (ATTR. size () == 0)) {attrs = ICTX.Getattributes (hostname, New String [] {"a"}); attr = Attrs.get ("A"); if (attr == NULL) Throwing NamingExcepti (ON (ON "No Match for name '" + Hostname + "'");} // huzzah! We have machnes to try. nt to be absolutely // Correct. This is left as anrcise for anyone who cares. ArrayList res = New ArrayList (); OST; string x = (string) en.next ( );; / / ""); // The fix **************************************************************************************************************************************************************************************************************************, (f [1] .ndswith (".") Mailhost = f [1] .substring (0, (f [1]. () - 1)); else mailhost = f [1]; // The fix **************** res.add (maylhost);} Return Res;} Public Static Boolean IsaddressValid (String Address) {// Find The SEPARTOR THE DMain Name Int POS = address.indexof ( '@'); // if the address does not contain an '@', it is not value if (pOS == -1) Return false; // isolate the domain/machine name and get a list of mail e XChangers String Domain = address.substring (++ POS); ArrayList mxlist = null; try {mxlist = getmx (domain);} Catch (namingexception ex) {Return false; Can send mail to the domain, doesn'T mean That the // address is value, but if we can'T, it's aour sign that it isn't if (mxlist.size () == 0) Return false; // now, do the smtp value, try eari L exchanger without we get // a positive accountance. It * May * be posSible for one mx to all // a message [Store and forwarder for exce] and Another [like // the a CTual Mail Server] To Reject it. This is why We really ome // to take the preference into account. For (int mx = 0; mx <mxlist.size (); mx ++) {boolean value = false; = New Socket ((String ) mxlist.get (mx), 25); bufferedReader RDR = new bufferedReadr W outputStreamWriter (SKT.GetoutPutstream ())); res = hear (RDR (RDR ); if (res! = 220) Throwing New Exception ("Invalid Header"); say (WTR, "EHLO RGAgnon.Com"); res = hear (RDR); if (res! = 250) Throw new exception ("" Not eSMTP "); /// value the sender address say (WTR," Mail from: <[email protected]> "); res = hear (rdr); if (res! = 250) Throwing New Exception (" Sender TED "); say (wtr," rcpt to: <" + address +"> ">"); res = hear (rdr); // be polite say (wtr, "rset"); hear (RDR); say (WTR, "Quit"); hear (rdr); if (res! = 250) Throwing New Exception ("Address is not value!"); Valid = TRUE; rdr.close (); wtr.close (); SKT.Close ( );} Catch (Exception EX) {// Do Nothing But Try NEXT EX.PRINTSTACKTRACE ();} Finally {if (Valid) Return True;}} Public Atic void main (String ARGS []) { String testdata [] = {"[email protected]", "[email protected]", "government address " [email protected] "// Failure of this method}; for (int ctr = 0; ctr <testdata.length; ctr ++) {system.out.println (TESTDATA [ctr]+" is vali D? " + ISADDRESSVALID (testdata [ctr]);} Return;}}The above is the logic of determining whether the mail address is legal. If the mail address is illegal, the mail address is removed from the recipient list.
Private Static String [] Removeinvalidateaddress (String [] addresses, String Mailfrom) {ArrayList <string> Validateaddresses = New ArrayList <STRING> (); String normalAddress = Null; int code; SMTPTRANSPORT SMPTTRARANS = NULL; if (StringUtils.IsEMPTY (Mailfrom ) || null == addresses) {Return New String [0];} String Sendcmd = "Mail from:" + Normalizeaddress (Mailfrom); Try {SMPTTRANS = (SMTPTRANSPORT) esion.gettransport ("smtp"); SMPTTRANS.CONNECT (); Code = SMPTTRANS.SIMPLECOMMAND (Sendcmd); If (Code! = 250 && Code! = 251) {Logger.error ("Send from Invalidate" + Mailfrom);} Else {for (string a DDRESS: Addressses) {normaladdress = normalizeaddress (address); string cmd = "rcpt to:" + normaladdress; code = smpttrans.simpleCommand (cmd); if (code == 250 || code == 251) Dateaddresses.add (address);}}} } Catch (MessagingException E) {Logger.error ("Validate Mail Address Error. Send from" + Mailfrom, E); String [validateaddresses.size ()]; Return Result; } Private Static String Normalizeaddress (String Addr) {if (! Addr. Startswith ("<") && (! addr.endswith (">"))) n addr ;}The above is all the contents of this article. I hope everyone can understand and help everyone.