zoukankan      html  css  js  c++  java
  • spring利用javamail,quartz定时发送邮件 <转>


    spring提供的定时发送邮件功能,下面是一个简单的例子以供大家参考,
    首先从spring配置文件开始吧:

       <!-- *****定时任务开始************** -->
       <!-- 定时发送邮件填写timesheet通知-->
            <bean id="JobServiceImpl" class="com.mdcl.timecard.mail.impl.MailManager"/>

       <!-- 定义调用对象和调用对象的方法 -->
            <bean id="projobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
                <property name="targetObject">
                    <ref bean="JobServiceImpl"/>
                </property>
                <property name="targetMethod">
                    <value>sendTimeSheetAlert</value>
                </property>
            </bean>
        <!-- 定义触发时间 -->
            <bean id="timecardSendMail" class="org.springframework.scheduling.quartz.CronTriggerBean">
                <property name="jobDetail">
                    <ref bean="projobtask"/>
                </property>
                <!-- cron表达式 -->
                <property name="cronExpression">
                    <value>0 0 0 ? * 5</value>
                </property>
            </bean>
          
         <!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 -->
           <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

                <property name="triggers">
                    <list>
                        <ref bean="timecardSendMail"/>
                        <!-- <ref bean="doTime"/> -->
                    </list>
                </property>
            </bean>

    Java代码:
    你讲在下面的代码中找到上面红色标注的那个方法
    package com.mdcl.timecard.mail.impl;

    import java.util.ArrayList;
    import java.util.List;

    import javax.mail.MessagingException;

    import org.apache.commons.lang.StringUtils;
    import org.apache.log4j.Logger;

    import com.mdcl.timecard.action.BaseAction;
    import com.mdcl.timecard.mail.IMailManager;
    import com.mdcl.timecard.po.Resource;
    import com.mdcl.timecard.service.IResourceService;
    import com.mdcl.timecard.util.StringUtil;
    import com.mdcl.timecard.util.SystemManager;

    public class MailManager  implements IMailManager {

        SimpleHtmlMailSender messageSender;
        private static final Logger logger = Logger.getLogger(MailManager.class);
        
        public void sendMail() {
      
        }

        public void sendTimeSheetAlert() {
      //获取资源列表
      List<Resource> list = new ArrayList<Resource>();
      //从配置文件获取发件人
      String emailfrom = SystemManager.getInstance().getPropValue("emailfrom");
      //从配置文件获取连接地址
      String wabappurl = SystemManager.getInstance().getPropValue("wabappurl");
      String text = "<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body><a href='"+wabappurl+"'>please add timesheet!</a></body></html>";
      // 取出所有资源做循环
      Resource resource = new Resource();
      resource.setEmail("verygoodverypowerful@gmail.com");
      list.add(resource);
      int i = 0;
      for (Resource listElement : list) {
          if(StringUtils.isNotBlank(listElement.getEmail())){
        messageSender.sendMessage(listElement.getEmail(), emailfrom, "please Add timesheet", text);
        logger.info("系统已经通知用户:"+listElement.getName()+"填写本周timesheet 。email:"+listElement.getEmail()+"已经发送成功。");
          }
        }
        }

        public SimpleHtmlMailSender getMessageSender() {
                 return messageSender;
        }

        public void setMessageSender(SimpleHtmlMailSender   messageSender) {
                 this.messageSender = messageSender;
        }

    }
    IMailManager.java:
    package com.mdcl.timecard.mail;

    public interface IMailManager {

    void sendMail();

    void sendTimeSheetAlert();
    }
    BaseMailSender.java
    package com.mdcl.timecard.mail.impl;

    import org.springframework.mail.javamail.JavaMailSender;
    public abstract class BaseMailSender {
    protected String to;
    protected String from;
    protected String subject;
    protected JavaMailSender sender;//当发送MIMI消息时用到的类

    public void setTo(String to) {
      this.to = to;
    }
    public void setFrom(String from) {
      this.from = from;
    }
    public void setSubject(String subject) {
      this.subject = subject;
    }
    public void setJavaMailSender(JavaMailSender sender) {
      this.sender = sender;
    }
    }
    配置文件
    mail.properties
    mail.host=xxxx
    mail.username=xxxx
    mail.password=xxxx
    conf.properties
    emailfrom=Emali地址
    emailto=Email地址
    wabappurl = 填写链接ui
  • 相关阅读:
    C# 实现 Snowflake算法生成唯一性Id
    kafka可视化客户端工具(Kafka Tool)的基本使用(转)
    docker 安装kafka
    Model类代码生成器
    使用docker 部署rabbitmq 镜像
    Vue 增删改查 demo
    git 提交代码到库
    Android ble蓝牙问题
    mac 配置 ssh 到git (Could not resolve hostname github.com, Failed to connect to github.com port 443 Operation timed out)
    okhttp
  • 原文地址:https://www.cnblogs.com/a757956132/p/4238033.html
Copyright © 2011-2022 走看看