Spring功能越来越多了,用起来还很舒服方便,Quartz实现的定时任务就是一个。

首先是配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- schedulerFactory -->
    <bean >
            <list>

      <!-- 有多少定时任务在这里写几个 -->
                <ref local="cronTriggerClaim" />
            </list>
        </property>
    </bean>
    
    <!-- 1.AutoClaimReminderMailService 第一个定时任务-->
    <bean />  <!-- 这里设定定时任务的具体方法-->
    </bean>
    

   <!-- 这个类就是自己写的了,注意要实现上面提到的sendRemiderMail方法,其它可自便-->
    <bean ><value>unknown5@163.com,unknown6@163.com</value></property>
    </bean>    
</beans>

下面是com.ibm.XXX.service.auto.AutoClaimReminderMailService类:

public class AutoClaimReminderMailService{
    private static Logger logger = Logger.getLogger(AutoClaimReminderMailService.class);
    
    public void sendRemiderMail() {
        String title="[Need Your Action!]Claim Reminder";
        
        StringBuilder sb=new StringBuilder();
        sb.append("<p>Hi Guys:</p>");
        sb.append("<p><B>Here is the claim reminder bell kindly for your action, pls submit your labor claim in ILC/Cats within today for this week, thanks your cooperation!</B></p>");   

            sendMail(title,sb.toString());
    }

 

    protected String smtpServer;
    protected String smtpUsername;
    protected String smtpPassword;
    protected String fromMailAddress;
    protected String toMailAddress;
    protected String ccMailAddress;
    protected String bccMailAddress;
    
    /**
     * 无参构造函数
     */
    public AutoClaimReminderMailService(){
        
    }

/**
     * 发送邮件的关键函数
     *
     * @param title
     * @param content
     * @return
     * @throws Exception
     */
    protected boolean sendMail(String title,String content) throws Exception{
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", smtpServer);
        // 获得邮件会话对象
        Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(smtpUsername, smtpPassword));
        /** *************************************************** */
        // 创建MIME邮件对象
        MimeMessage mimeMessage = new MimeMessage(session);
        mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
        
        mimeMessage.setRecipients(Message.RecipientType.TO, getInternetAddressArr(toMailAddress));// To收件人
        mimeMessage.setRecipients(Message.RecipientType.CC, getInternetAddressArr(ccMailAddress));// Cc收件人
        mimeMessage.setRecipients(Message.RecipientType.BCC, getInternetAddressArr(bccMailAddress));// Bcc收件人
        
        mimeMessage.setSubject(title);
        mimeMessage.setSentDate(new Date());// 发送日期
        Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
        /** *************************************************** */
        BodyPart bodyPart = new MimeBodyPart();// 正文
        bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=utf8"));// 网页格式
        mp.addBodyPart(bodyPart);
        mimeMessage.setContent(mp);// 设置邮件内容对象
        Transport.send(mimeMessage);// 发送邮件       
        
        return true;
    }
    
    protected InternetAddress[] getInternetAddressArr(String mialAddr) throws Exception{
        String[] arr=mialAddr.split(",");
        
        InternetAddress[] retval=new InternetAddress[arr.length];
        
        for(int i=0;i<arr.length;i++){
            retval[i]=new InternetAddress(arr[i]);
        }
        
        return retval;
    }
}

 

就到这里,再见吧。

相关文章:

  • 2018-10-10
  • 2022-01-28
  • 2022-01-20
  • 2021-04-23
  • 2022-12-23
  • 2021-06-05
  • 2021-08-03
  • 2021-09-29
猜你喜欢
  • 2021-09-01
  • 2022-12-23
  • 2021-08-22
  • 2021-05-22
  • 2021-05-27
相关资源
相似解决方案