Java is super simple to send emails (dynamic control of the number of recipients), java sends emails
Email sending implementation
The following rack packages need to be introduced in advance, the most important rack package is jodd-3.7 this
Above frame package: http://pan.baidu.com/s/1kVs7Tyv extraction password: h22x
A new utilcategory is created, in which emails.txt is used to dynamically configure the sending object for sending emails.
Package quartz; import java. io. bufferedReader; import java. io. inputStream; import java. io. inputStreamReader; import java. text. simpleDateFormat; import java. util. arrayList; import java. util. date; import java. util. list;/***** @ author DONG */public class Util {public static final String format = "HH: mm"; public static final SimpleDateFormat sdf = new SimpleDateFormat (format ); public static String content = "The following Electric piles have been disconnected for more than 1 hour" + "[" + sdf. format (new Date () + "]"; // public static Date lastSend = null; public static List getEmailList () {return getList ("emails.txt ");} public static List getList (String fileName) {try {InputStream is = Util. class. getResourceAsStream (fileName); InputStreamReader isr = new InputStreamReader (is); BufferedReader br = new BufferedReader (isr); List list = new ArrayList (); String Line = ""; while (line = br. readLine ())! = Null) {if (! "". Equals (line. trim () list. add (line) ;}br. close (); isr. close (); is. close (); return list;} catch (Exception e) {e. printStackTrace ();} return null ;}}
The above code can be directly copied and reused
The next step is to send an email.
Public void run () {List <String> emails = Util. getEmailList (); // get the set of email sending objects if (emails. isEmpty () {System. out. println ("no email receiver"); return;} String from = "********* @ sina.com"; // user name, log on to the email account String psw = "***********"; // password String [] to = emails. toArray (new String [0]); Email email = Email. create (). from (from ). to (). subject ("") // subject of the email. addText (Util. content); // mail content SmtpServer smtpServer = SmtpServer. create ("smtp.sina.com") // call the Sina mail server. authenticateWith (from, psw); SendMailSession session = smtpServer. createSession (); session. open (); session. sendMail (email); // execute the sending session. close (); System. out. println ("-- email send success. receivers: "+ Arrays. deepToString (emails. toArray ()));}
Call the run method where an email is to be sent. The preceding is a simple email sending example.
In the next article, we will add how to customize and add mail content.