利用spring定时器发送定时邮件
利用spring定时器发送定时邮件
2015年10月16日 16:56:15
阅读数:4466
spring 的org.springframework.mail包提供的对邮件的支持。
1.封装一个方法用于发送邮件的方法:
[java] view plain copy
- 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.定义一个发送邮件的定时类
[java] view plain copy
- 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配置文件定时调用发送邮件的方法。
[html] view plain copy
- <?**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**>
- <**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**>
- </**bean**>
- </**beans**>
sping配置文件关联图解:
还没有评论,来说两句吧...