Spring整合Quartz定时发送邮件

本文介绍如何使用Quartz结合Spring框架实现定时邮件发送功能。通过整合Quartz和Spring,可以实现每分钟发送一封邮件,并连续发送十次的任务。文章详细展示了所需的核心jar包以及关键代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能描述:刚开始接触Quartz,试着用Quartz整合spring实现每隔一分钟发送一封邮件连续发送10次
        核心jar:
                    邮件发送:commons-email-1.2.jar mail.jar(必须的)
                    quartz:quartz-all-1.8.3.jar quartz-all-1.8.3/lib/下所有jar
                    spring:spring-context-support.ajr(必须的)
        只贴出核心代码:
        Email发送:使用apache commons-email跟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;

/** */ /**
 * 功能描述:此代码主要实现邮件发送功能
 * 
@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(
" 测试Quartz " );
        email.setHostName(
" smtp.163.com " );
        email.setAuthentication(
" xxxx " " xxxx " );
        email.setTo(list);
        email.setContent(
" <h1>Hello,把凤姐许配给你,你看咋样?</h1> " " text/html;charset=utf-8 " );
        email.send();
    }

    
    
}

        定义调度工作任务:继承自org.springframework.scheduling.quartz.QuartzJobBean次类在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;

/** */ /**
 * 发送email任务
 * 
@author  coder
 *
 
*/

public   class  SendEmailJob  extends  QuartzJobBean {
    
    @Override
    
protected   void  executeInternal(JobExecutionContext context)
            
throws  JobExecutionException  {
        
try   {
            
// 调用邮件发送代码
            SendSimplEmail.sendMail();
        }
  catch  (Exception e)  {
            e.printStackTrace();
        }

    }


    

}

        spring核心配置文件

<!--  定义调度工作任务  -->
    
< bean  id ="quantzjobBean"  class ="org.springframework.scheduling.quartz.JobDetailBean" >
        
< property  name ="jobClass" >
            
<!--  实现了org.springframework.scheduling.quartz.QuartzJobBean的JobBean  -->
            
< value > com.ssh.quantz.SendEmailJob </ value >
        
</ property >
        
<!--  调用业务逻辑  -->
        
<!--   
        <property name="jobDataAsMap">
            <map>
                <entry key="biz">
                    <ref bean="users"/>
                </entry>
            </map>
        </property>
        
-->
    
</ bean >

    
<!--  触发任务条件  -->
    
< bean  id ="simpletriggerbean"  class ="org.springframework.scheduling.quartz.SimpleTriggerBean" >
        
< property  name ="jobDetail"  ref ="quantzjobBean" ></ property >
        
<!--  延迟一分钟启动  -->
        
< property  name ="startDelay" >
            
< value > 60000 </ value >
        
</ property >
        
<!--  每隔2分钟调用一次  -->
        
< property  name ="repeatInterval" >
            
< value > 60000 </ value >
        
</ property >
        
<!--  执行10次  -->
        
< property  name ="repeatCount" >
            
< value > 10 </ value >
        
</ property >
        
    
</ bean >
    
    
<!--  启动调度  -->
    
< bean  id ="startQuartz"  class ="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
        
< property  name ="triggers" >
            
< list >
                
< ref  bean ="simpletriggerbean" />
            
</ list >
        
</ property >
    
</ bean >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值