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

绝地灬酷狼 2022-05-24 01:06 284阅读 0赞
利用spring定时器发送定时邮件

2015年10月16日 16:56:15

阅读数:4466

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

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

[java] view plain copy

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

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

[java] view plain copy

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

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. <**bean id=”schedulerFactory” class=”org.springframework.scheduling.quartz.SchedulerFactoryBean”>**
  8. <**property name=”triggers”>**
  9. <**list**>
  10. <**ref bean=”autoStatistics_time” />**
  11. </**list**>
  12. </**property**>
  13. </**bean**>
  14. <**bean id=”autoStatisticsTask” class=”com.ql.v2.controller.tasks.AutoStatisticsTask”/>**
  15. <**bean id=”autoStatisticsTaskProperties” class=”org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean”>**
  16. <**property name=”targetObject” ref=”autoStatisticsTask”></property**>
  17. <**property name=”targetMethod” value=”autoStatisticWxInfo”></property**>
  18. <**property name=”concurrent” value=”false”></property**>
  19. </**bean**>
  20. <**bean id=”autoStatistics_time” class=”org.springframework.scheduling.quartz.CronTriggerBean”>**
  21. <**property name=”jobDetail” ref=”autoStatisticsTaskProperties”></property**>
  22. <**property name=”cronExpression” value=”0 0/1 * * * ?”></property**>
  23. </**bean**>
  24. </**beans**>

sping配置文件关联图解:

Center

发表评论

表情:
评论列表 (有 0 条评论,284人围观)

还没有评论,来说两句吧...

相关阅读

    相关 利用java实现发送邮件

    电子邮件的应用非常广泛,常见的如在某网站注册了一个账户,自动发送一封激活邮件,通过邮件找回密码,自动批量发送活动信息等。很显然这些应用不可能和我们自己平时发邮件一样,先打开浏览

    相关 利用java实现发送邮件

    电子邮件的应用非常广泛,常见的如在某网站注册了一个账户,自动发送一封激活邮件,通过邮件找回密码,自动批量发送活动信息等。很显然这些应用不可能和我们自己平时发邮件一样,先打开浏览