利用spring定时器发送定时邮件 利用spring定时器发送定时邮件

利用spring定时器发送定时邮件

  spring 的org.springframework.mail包提供的对邮件的支持。

1.封装一个方法用于发送邮件的方法:

[java]  view plain  copy
  1. package com.ql.v2.utils;  
  2.   
  3. import java.util.Properties;  
  4. import javax.mail.internet.MimeMessage;  
  5. import org.springframework.mail.SimpleMailMessage;  
  6. import org.springframework.mail.javamail.JavaMailSenderImpl;  
  7. import org.springframework.mail.javamail.MimeMessageHelper;  
  8. /** 
  9.  isValate:是否校验(或者授权):true 
  10.  to:邮件接收者地址数组。:{"***@qq.com","***@qq.com"},因为是数组所以支持群发。 
  11.  subject:邮件主题 
  12.  context:邮件内容 
  13.  */  
  14.      public static void sendEmails( String isValate, String[] to, String subject, String context) throws Exception {  
  15.         //邮件服务器的配置信息  
  16.         JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();  
  17.         senderImpl.setHost("smtp.exmail.qq.com");//发件服务器HOST  
  18.         // 根据自己的情况,设置username  
  19.         senderImpl.setUsername("***");//发件人名称  
  20.         // 根据自己的情况, 设置password  
  21.         senderImpl.setPassword("***");//发件邮箱密码  
  22.           
  23.         Properties pp = new Properties();  
  24.         pp.put("mail.smtp.auth", isValate);//是否校验(或者授权):true  
  25.         senderImpl.setJavaMailProperties(pp);  
  26.           
  27.         // 获取邮件的样板  
  28.         SimpleMailMessage msg = new SimpleMailMessage();  
  29.         msg.setFrom("[email protected]");// 发邮件邮箱  
  30.         msg.setTo(to);  
  31.         msg.setSubject(subject);// 邮件主题  
  32.         msg.setText(context);  
  33.           
  34.         MimeMessage mimeMsg = senderImpl.createMimeMessage();  
  35.         MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true"UTF-8");  
  36.         helper.setTo(msg.getTo());//邮件接收地址  
  37.         helper.setSubject(msg.getSubject());//邮件主题  
  38.         helper.setFrom(msg.getFrom(), "别名");//邮件发送人-别名  
  39.         helper.setText(msg.getText(), true);//邮件内容  
  40.         senderImpl.send(mimeMsg);//发送邮件  
  41.     }  

2.定义一个发送邮件的定时类


[java]  view plain  copy
  1. package com.ql.v2.controller.tasks;  
  2.   
  3. import java.io.File;  
  4. import org.apache.commons.io.FileUtils;  
  5. import org.springframework.core.io.ClassPathResource;  
  6. import com.ql.v2.utils.SendEmailUtil;  
  7.   
  8. /** 
  9.  * 自动统计类 
  10.  * @author huayafei 
  11.  * 
  12.  */  
  13. public class AutoStatisticsTask {     
  14.     /** 
  15.      * 自动统计微信端定存宝、活期宝的投资金额与人数信息。 
  16.      */  
  17.     public void autoStatisticWxInfo(){  
  18.         try {  
  19.             //读取文件,然后发送邮件。  
  20.             ClassPathResource resource=new ClassPathResource("/");  
  21.             String filepath=resource.getFile().getParent()+"/other_pages/statistic.html";  
  22.             //利用oapche提供的方法把一个文件读取成字符串  
  23.             String html=FileUtils.readFileToString(new File(filepath),"UTF-8");  
  24.             /* 
  25.              * 群发邮件 
  26.              */  
  27.             String isValate = "true";//邮件授权  
  28.             String[] to = {"***@qinlian.me","***@qq.com"};//收件人  
  29.             String subject = "微信端投资统计记录";//邮件主题  
  30.             //调用发送邮件的方法  
  31.             SendEmailUtil.sendEmails(isValate, to, subject, html);  
  32.         } catch (Exception e) {  
  33.             throw new RuntimeException(e.getMessage(),e);  
  34.         }  
  35.     }  
  36. }  

3.设置spring配置文件定时调用发送邮件的方法。

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">  
  7.       
  8.     <!-- 总调度,用于启动定时器 -->  
  9.     <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  10.         <property name="triggers">  
  11.             <list>  
  12.                 <ref bean="autoStatistics_time" />  
  13.             </list>  
  14.         </property>  
  15.     </bean>  
  16.       
  17.     <!-- 注入调度的object -->  
  18.     <bean id="autoStatisticsTask" class="com.ql.v2.controller.tasks.AutoStatisticsTask"/>  
  19.       
  20.     <!--自动发送统计数据-->  
  21.     <bean id="autoStatisticsTaskProperties" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  22.         <property name="targetObject" ref="autoStatisticsTask"></property><!-- 定时器类 -->  
  23.         <property name="targetMethod" value="autoStatisticWxInfo"></property><!-- 定时器类的具体处理方法 -->  
  24.         <property name="concurrent" value="false"></property> <!-- 定时器是否支持并发 -->  
  25.     </bean>  
  26.       
  27.     <!-- 自动发送统计数据 -->  
  28.     <bean id="autoStatistics_time" class="org.springframework.scheduling.quartz.CronTriggerBean">  
  29.         <property name="jobDetail" ref="autoStatisticsTaskProperties"></property>  
  30.         <property name="cronExpression" value="0 0/1 * * * ?"></property> <!--每1分钟执行一次 -->  
  31.     </bean>  
  32. </beans>  

sping配置文件关联图解:














猜你喜欢

转载自blog.csdn.net/zhufengyan521521/article/details/80351928
今日推荐