首先从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代码:
你讲在下面的代码中找到上面红色标注的那个方法
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:
public interface IMailManager
{
void sendMail();
void
sendTimeSheetAlert();
}
BaseMailSender.java
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.username=xxxx
mail.password=xxxx
conf.properties
emailto=Email地址
wabappurl = 填写链接ui