Function Description: I started to contact Quartz and tried to use Quartz to integrate spring to send an email 10 times in a row every minute.
Core jar:
Mail: commons-email-1.2.jar mail. jar (required)
Quartz: quartz-all-1.8.3.jar quartz-all-1.8.3/lib/all jar
Spring: spring-context-support.ajr (required)
Paste only the core code:
Email sending: Use apache commons-email and mail
Package com. ssh. commonsemail;
Import java. util. ArrayList;
Import java. util. Date;
Import java. util. List;
Import javax. mail. internet. InternetAddress;
Import org. apache. commons. mail. SimpleEmail;
/***//**
* Function Description: This code mainly implements the mail sending function.
* @ Author coder
*
*/
Public class SendSimplEmail {
Public static void sendMail () throws Exception {
List <InternetAddress> list = new ArrayList <InternetAddress> ();
List. add (new InternetAddress ("313698683@qq.com "));
List. add (new InternetAddress ("184675420@qq.com "));
SimpleEmail email = new SimpleEmail ();
Email. setFrom ("184675420@163.com ");
Email. setCharset ("UTF-8 ");
Email. setSentDate (new Date ());
Email. setSubject ("test Quartz ");
Email. setHostName ("smtp.163.com ");
Email. setAuthentication ("xxxx", "xxxx ");
Email. setTo (list );
Email. setContent ("Email. send ();
}
}
Define scheduling job: Inherit from org. springframework. scheduling. quartz. QuartzJobBean times class in spring-context-support.jar
Package com. ssh. quantz;
Import org. quartz. JobExecutionContext;
Import org. quartz. JobExecutionException;
Import org. springframework. scheduling. quartz. QuartzJobBean;
Import com. ssh. commonsemail. SendSimplEmail;
/***//**
* Send an email task
* @ Author coder
*
*/
Public class SendEmailJob extends QuartzJobBean {
@ Override
Protected void executeInternal (JobExecutionContext context)
Throws JobExecutionException {
Try {
// Call the email sending code
SendSimplEmail. sendMail ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
Spring core configuration file
<! -- Define a scheduling task -->
<Bean id = "quantzjobBean" class = "org. springframework. scheduling. quartz. JobDetailBean">
<Property name = "jobClass">
<! -- Implements JobBean of org. springframework. scheduling. quartz. QuartzJobBean -->
<Value> com. ssh. quantz. SendEmailJob </value>
</Property>
<! -- Call business logic -->
<! --
<Property name = "jobDataAsMap">
<Map>
<Entry key = "biz">
<Ref bean = "users"/>
</Entry>
</Map>
</Property>
-->
</Bean>
<! -- Trigger task condition -->
<Bean id = "simpletriggerbean" class = "org. springframework. scheduling. quartz. SimpleTriggerBean">
<Property name = "jobDetail" ref = "quantzjobBean"> </property>
<! -- One-minute delay -->
<Property name = "startDelay">
<Value> 60000 </value>
</Property>
<! -- Call every 2 minutes -->
<Property name = "repeatInterval">
<Value> 60000 </value>
</Property>
<! -- Execute 10 times -->
<Property name = "repeatCount">
<Value> 10 </value>
</Property>
</Bean>
<! -- Start scheduling -->
<Bean id = "startQuartz" class = "org. springframework. scheduling. quartz. SchedulerFactoryBean">
<Property name = "triggers">
<List>
<Ref bean = "simpletriggerbean"/>
</List>
</Property>
</Bean>