Java is super simple to send emails (dynamic control of the number of recipients), java sends emails

Source: Internet
Author: User
Tags email account

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.