1. Java uses the corporate mail server to send mail
1 /**2 * Send mail3 */4 Public voidSendMail (string receivers, string content, string title) {5 Try {6 //Corporate mail server address7URL url =NewURL ("Corporate Mail server Address");8 //Set Connection9HttpURLConnection connection =(HttpURLConnection) url.openconnection ();TenConnection.setdooutput (true); OneConnection.setdoinput (true); AConnection.setrequestmethod ("POST"); -Connection.setusecaches (false); -Connection.setinstancefollowredirects (true); theConnection.setrequestproperty ("Content-type", "Application/json"); -Connection.setrequestproperty ("Connection", "keep-alive"); - Connection.connect (); - //set the message recipient, title, Content +DataOutputStream out =NewDataOutputStream (Connection.getoutputstream ()); -map<string, string> jsonmap =NewHashmap<string, String> (3); +Jsonmap.put ("to", receivers);//Mail Recipient AJsonmap.put ("Subject", Converttoiso_8859_1 (title));//Message Subject atJsonmap.put ("Content", converttoiso_8859_1 (content));//message body -Jsonobject jsonobj =Jsonobject.fromobject (jsonmap); - //Send - out.writebytes (jsonobj.tostring ()); - Out.flush (); - out.close (); in //return content -BufferedReader reader =NewBufferedReader (NewInputStreamReader (Connection.getinputstream ())); toString result = ""; + String Line; - while(line = Reader.readline ())! =NULL) { theResult + =Line ; * } $ //Logging message return contentPanax Notoginseng if(logger.isinfoenabled ()) { - logger.info (result); the } + //parsing the returned content AJsonobject Resultjson =jsonobject.fromobject (result); theString code = resultjson.getstring ("code"); + if("$". Equals (code)) {//200 indicates successful send, delete temporary table data if send is successful -Logger.info ("e-mail success! "); $}Else { $Logger.info ("Send mail failed, wait for next send!") "); - } - the}Catch(malformedurlexception e) { -Logger.error ("Message sent Failed", e);Wuyi the}Catch(IOException e) { -Logger.error ("Message sent Failed", e); Wu } -}View Code
Java uses the corporate mail server to send mail