In Thursday, the time to go to a school on duty, the main job is to help teachers send documents, do some miscellaneous work. There was no work on that day, so I remembered the ability to send and receive mail with Java. A few days ago contacted a little quartz frame, used to realize the function of scheduled start task. So, here they combine, realize the time to send mail, to the goddess of love confession of the usefulness (Yiyin).
I have not studied the quartz framework, here is mainly from a practical point of view. When used, you need to write code for the two parts of the task class and the trigger class. The task class is the functional part you want to implement, and you need to implement the job interface. The trigger class sets the scheduled execution time for the task. In the Java implementation of e-mail, the Outbox use QQ mailbox, everyone may be a QQ mailbox. What's more, we have to lament Java's powerful class library and the contributions of many Java workers. Code for the task class:
1 ImportOrg.quartz.Job;2 ImportOrg.quartz.JobExecutionContext;3 Importorg.quartz.JobExecutionException;4 ImportJava.util.Calendar;5 ImportJava.text.SimpleDateFormat;6 Importjava.util.Date;7 Importjava.lang.InterruptedException;8 ImportJava.util.Random;9 Importjava.util.Properties;Ten ImportJavax.mail.*; One Importjavax.mail.internet.*; A Public classMailjobImplementsJob - { - Public voidExecute (jobexecutioncontext context) the throwsjobexecutionexception { - //recipients, title, and text content -String to = "#######@126.com";//fill out who you want to send -String title =createtitle (); +String Text =CreateText (); - //Setting Properties +Properties props =NewProperties (); A //QQ Mailbox Outgoing server and port atProps.put ("Mail.smtp.host", "smtp.qq.com"); -Props.put ("Mail.smtp.socketFactory.port", "465"); -Props.put ("Mail.smtp.socketFactory.class", "Javax.net.ssl.SSLSocketFactory"); -Props.put ("Mail.smtp.auth", "true"); -Props.put ("Mail.smtp.port", "25"); -Session session =session.getdefaultinstance (Props, in NewJavax.mail.Authenticator () { - protectedpasswordauthentication getpasswordauthentication () { to //fill in your QQ email user name and password + return NewPasswordauthentication ("******* @qq. com", "###***%%%"); - } the }); *MimeMessage message =NewMimeMessage (session); $ //Flag is used here to flag whether the sender is successful (sometimes not on the server).Panax Notoginseng //if not, continue sending until the send succeeds. - intFlag = 0; the { + Try { ASet the sender, recipient, subject and text content, and send theMessage.setfrom (NewInternetAddress ("******* @qq. com"));//fill in your own QQ mailbox, and the same as above +Message.addrecipient (Message.RecipientType.TO,Newinternetaddress (to)); - Message.setsubject (title); $ message.settext (text); $System.out.println ("Preparing sending mail ..."); - System.out.println (text); - transport.send (message); theFlag = 1; -SYSTEM.OUT.PRINTLN ("Message sent successfully");Wuyi}Catch(Exception e) { theFlag = 0; - } Wu} while(flag = = 0); - } About //The following two methods are used to randomly combine headings and text content. The text content is randomly combined by four parts. $ //Generate title - PublicString Createtitle () { -String[] titles = {' Love ', ' I Love You ', ' Miss you ', ' My baby '}; -Random Randt =NewRandom (System.currenttimemillis ()); AString title = Titles[randt.nextint (titles.length)]; + returntitle; the } - //The text content is generated and the text content is randomly combined in four parts. $ PublicString CreateText () { the //The name is purely fictitious, if there is a similarity (certainly will have), is purely coincidental. theString[] Parts1 = {"Xiao Jing, hello." "," Xiao Jing, are you okay? "," Xiao Jing, what's the weather like over there? "}; thestring[] Parts2 = { the"It's been a long time since I last met," he thought. ", -"The flow of time to grind my love for you." ", in"I still remember the smile you had in front of Tiananmen Square. " the }; theString[] Parts3 = {"Today, I am daring to pursue you." ", About"I am not afraid to dare to say to you, I love you." ", the"At the moment, the moon represents my heart. " the }; thestring[] Parts4 = { +"In the future, my heart still belongs to you." ", -"Good want in the future with you slowly grow old, of course, in my heart you will not be old." " the };BayiRandom Randt =NewRandom (System.currenttimemillis ()); theString Text =Parts1[randt.nextint (parts1.length)] the+Parts2[randt.nextint (parts2.length)] -+Parts3[randt.nextint (parts3.length)] -+Parts4[randt.nextint (parts4.length)]; the returntext; the } the the}
Code for the trigger:
1 ImportOrg.quartz.CronScheduleBuilder;2 ImportOrg.quartz.JobBuilder;3 ImportOrg.quartz.JobDetail;4 ImportOrg.quartz.Scheduler;5 ImportOrg.quartz.Trigger;6 ImportOrg.quartz.TriggerBuilder;7 Importorg.quartz.impl.StdSchedulerFactory;8 ImportJava.util.Random;9 Public classCrontriggerexampleTen { One Public Static voidMain (string[] args)throwsException A { - //Create a Work object -Jobdetail job = Jobbuilder.newjob (mailjob.class) the. Withidentity ("Dummyjobname", "group1"). Build (); - //for immediate testing, you can use the following code, which executes every 5 seconds - //int secdelta = 5; - //Trigger Trigger = Triggerbuilder + //. Newtrigger () - //. Withidentity ("Dummytriggername", "group1") + //. Withschedule ( A //cronschedulebuilder.cronschedule ("0/" + Secdelta + "* * * * *?")) at //. Build (); - //9 o ' Day in the morning (no more than 3 minutes) -Random Rand =NewRandom (System.currenttimemillis ()); - intSecdelta = Rand.nextint (60 * 3); - //To create a Trigger object -Trigger Trigger =Triggerbuilder in . Newtrigger () -. Withidentity ("Dummytriggername", "group1") to . Withschedule ( +Cronschedulebuilder.cronschedule (Secdelta + "0 9? * Sun-sat ")) - . Build (); the *Scheduler Scheduler =Newstdschedulerfactory (). Getscheduler (); $ Scheduler.start ();Panax Notoginseng //associating triggers with work - scheduler.schedulejob (Job, trigger); the } +}Outgoing mail-dependent packages: activation.jar,mail.jarquartz:http://www.quartz-scheduler.org/downloads/
The packages that are dependent on the message and the quartz that are downloaded under the Lib path are all placed under the Mylib path, and the Mylib path is located in the same directory as the Java file. When compiling and running, you can use the command:
Set classpath=mylib/*;.;
Javac Crontriggerexample.java
Java crontriggerexample
Java implementation QQ mailbox daily send mail regularly