利用spring定时器发送定时邮件
2015-10-16 16:56
701 查看
spring
的org.springframework.mail包提供的对邮件的支持。
1.封装一个方法用于发送邮件的方法:
2.定义一个发送邮件的定时类
3.设置spring配置文件定时调用发送邮件的方法。
sping配置文件关联图解:
的org.springframework.mail包提供的对邮件的支持。
1.封装一个方法用于发送邮件的方法:
package com.ql.v2.utils; import java.util.Properties; import javax.mail.internet.MimeMessage; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; /** isValate:是否校验(或者授权):true to:邮件接收者地址数组。:{"***@qq.com","***@qq.com"},因为是数组所以支持群发。 subject:邮件主题 context:邮件内容 */ public static void sendEmails( String isValate, String[] to, String subject, String context) throws Exception { //邮件服务器的配置信息 JavaMailSenderImpl senderImpl = new JavaMailSenderImpl(); senderImpl.setHost("smtp.exmail.qq.com");//发件服务器HOST // 根据自己的情况,设置username senderImpl.setUsername("***");//发件人名称 // 根据自己的情况, 设置password senderImpl.setPassword("***");//发件邮箱密码 Properties pp = new Properties(); pp.put("mail.smtp.auth", isValate);//是否校验(或者授权):true senderImpl.setJavaMailProperties(pp); // 获取邮件的样板 SimpleMailMessage msg = new SimpleMailMessage(); msg.setFrom("123123123@qq.com");// 发邮件邮箱 msg.setTo(to); msg.setSubject(subject);// 邮件主题 msg.setText(context); MimeMessage mimeMsg = senderImpl.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMsg, true, "UTF-8"); helper.setTo(msg.getTo());//邮件接收地址 helper.setSubject(msg.getSubject());//邮件主题 helper.setFrom(msg.getFrom(), "别名");//邮件发送人-别名 helper.setText(msg.getText(), true);//邮件内容 senderImpl.send(mimeMsg);//发送邮件 }
2.定义一个发送邮件的定时类
package com.ql.v2.controller.tasks; import java.io.File; import org.apache.commons.io.FileUtils; import org.springframework.core.io.ClassPathResource; import com.ql.v2.utils.SendEmailUtil; /** * 自动统计类 * @author huayafei * */ public class AutoStatisticsTask { /** * 自动统计微信端定存宝、活期宝的投资金额与人数信息。 */ public void autoStatisticWxInfo(){ try { //读取文件,然后发送邮件。 ClassPathResource resource=new ClassPathResource("/"); String filepath=resource.getFile().getParent()+"/other_pages/statistic.html"; //利用oapche提供的方法把一个文件读取成字符串 String html=FileUtils.readFileToString(new File(filepath),"UTF-8"); /* * 群发邮件 */ String isValate = "true";//邮件授权 String[] to = {"***@qinlian.me","***@qq.com"};//收件人 String subject = "微信端投资统计记录";//邮件主题 //调用发送邮件的方法 SendEmailUtil.sendEmails(isValate, to, subject, html); } catch (Exception e) { throw new RuntimeException(e.getMessage(),e); } } }
3.设置spring配置文件定时调用发送邮件的方法。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 总调度,用于启动定时器 --> <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="autoStatistics_time" /> </list> </property> </bean> <!-- 注入调度的object --> <bean id="autoStatisticsTask" class="com.ql.v2.controller.tasks.AutoStatisticsTask"/> <!--自动发送统计数据--> <bean id="autoStatisticsTaskProperties" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="autoStatisticsTask"></property><!-- 定时器类 --> <property name="targetMethod" value="autoStatisticWxInfo"></property><!-- 定时器类的具体处理方法 --> <property name="concurrent" value="false"></property> <!-- 定时器是否支持并发 --> </bean> <!-- 自动发送统计数据 --> <bean id="autoStatistics_time" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="autoStatisticsTaskProperties"></property> <property name="cronExpression" value="0 0/1 * * * ?"></property> <!--每1分钟执行一次 --> </bean> </beans>
sping配置文件关联图解:
相关文章推荐
- Spring MVC 教程,快速入门,深入分析――如何访问到静态的文件,如jpg,js,css?
- Java 反射机制浅谈
- Windows7部署Android开发环境傻瓜式教程(Eclipse+ADT) (转)
- Spring MVC 教程,快速入门,深入分析――springMVC-mvc.xml 配置文件片段讲解
- Java虚拟机详解02----JVM内存结构
- java入门第六天之跟屁虫小球
- Java中数据类型的大小与范围
- java处理html转义符号
- java连接webservice
- Java理论学时第三节。课后作业。
- Java反射机制1
- Zookeeper Api(java)入门与应用
- SpringMVC 介绍以及IDE插件安装配置
- Intellij14搭建Maven的SpringMVC记录
- 链表与哈希表基本概念及Java常用集合
- 创建应用时,对 androidannotations 的配置(Eclipse版本)
- java第六天 宿舍网坏了断更了 今天补上吧 for循环还有各种变态作业
- Struts2-表单标签
- myeclipse 2014 发布项目注意的问题
- myeclipse 2014 发布项目注意的问题