Implementation of sending messages
Need to introduce the following several racks in advance, the most important rack package is jodd-3.7 this
Above rack Package: Http://pan.baidu.com/s/1kVs7Tyv Extract Password: h22x
Create a new Util class, where emails.txt is used to dynamically configure the sending object to send mail
PackageQuartz;ImportJava.io.BufferedReader;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.Date;Importjava.util.List;/** * * @author Dong*/ Public classutil{ Public Static FinalString format = "hh:mm"; Public Static FinalSimpleDateFormat SDF =NewSimpleDateFormat (format); Public StaticString content = "The following electric pile has been disconnected for more than 1 hours" + "" "+ Sdf.format (NewDate ()) + "" "; //Send message content Public StaticDate Lastsend =NULL; Public StaticList getemaillist () {returnGetList ("Emails.txt"); } Public StaticList getList (String fileName) {Try{InputStream is= Util.class. getResourceAsStream (FileName); InputStreamReader ISR=NewInputStreamReader (IS); BufferedReader BR=NewBufferedReader (ISR); List List=NewArrayList (); String Line= ""; while(line = Br.readline ())! =NULL ){ if(!"". Equals (Line.trim ())) List.add (line); } br.close (); Isr.close (); Is.close (); returnlist; }Catch(Exception e) {e.printstacktrace (); } return NULL; }}
The above code can be directly copied and reused
The next thing to do is send mail.
Public voidrun () {List<String> emails = util.getemaillist ();//gets the collection of mail-sending objects if(Emails.isempty ()) {System.out.println ("No email receiver"); return; } String from= "******** @sina. com";//user name, login email accountString PSW = "**********";//PasswordString[] to = Emails.toarray (NewString[0]); Email Email=email.create (). from (from). Subject ("Time-out reminder of electric pile disconnection")//Message Subject. AddText (Util.content);//Message ContentSmtpServer SmtpServer= Smtpserver.create ("smtp.sina.com")//call Sina Mailbox Server. Authenticatewith (from, PSW); Sendmailsession Session=smtpserver.createsession (); Session.open (); Session.sendmail (email);//Execute SendSession.close (); System.out.println ("--email send success. Receivers: "+arrays.deeptostring (Emails.toarray ())); }
Call the Run method where you want to send the message. The above is an ultra-simple send email example, pro-test effective
Next, will supplement how to customize Add message content
Java Ultra-simple implementation of sending mail (can dynamically control the number of people sent)