Java Ultra-simple implementation of sending mail (can dynamically control the number of people sent)

Source: Internet
Author: User

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)

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.