除spring常用包外还用到的Jar包
quartz-1.6.5.jar mail.jar activation.jar
本人的业务需求是:
每月月末0点0时0分给指定的email地址发送月Excel报表,指定的email地址经常变化。
先说介绍一下我对Spring定时发送邮件的理解,有不对的地方请大家指正。
首先,Spring本身就嵌入了发送邮件和定时任务的功能。我们要做的就是导入相应的jar包,配置邮件属性和定时属性就可以了。下面是简单定时邮件的实现(网上搜一下“java定时邮件“,到处都是,为了方便大家我在这再重复一次)
1、将Spring的加载方式设置成在web.xml中加载,在web.xml加入
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-clss>
</listener>
2、 在Spring中配置邮件属性
<!—配置邮件发送端-->
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="邮件服务器地址"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!—配置邮件属性-->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="目标地址"/>
<property name="from" >
<list>
<value></value>
</list>
</property>
<property name="subject" value="test"/>
<property name="subject" value="test"/>
</bean>
<!—指定邮件发送的方法-->
<bean id="ms" class="com.open.quartz.MailService" autowire="byName"/>
3、 编写邮件发送的方法
public class MailService {
private MailSender mailSender;
private SimpleMailMessage mailMessage;//邮件模板public void sendMail() {
System.out.println("-----------发送邮件!---------");
SimpleMailMessage msg=new SimpleMailMessage(this.mailMessage); msg.setText("this is a test mail");
try {
mailSender.send(msg);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("-----------发送成功!---------");
} //省略set,get方法}
4、 配置定时发送属性,再回到Spring配置文件中
<bean id="reportJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="ms"/>
<property name="targetMethod" value="sendMail"/>
</bean>
<!--一个定时的调度器-->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean"><!--指定每月月末0点0分0秒进行发送-->
<property name="cronExpression" value="0 0 0 1 * ?"/>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger"/>
</list>
</property>
需要说明的是cronExpression的值得规则
例如:0 21 12 1 5 ?表示每年的5月1号12点21分发送,
例子一下就清楚了吧
我们的需求中要随时改变发送目标地址,并且邮件中还有附件,这两点功能上面都实现不了。好像还没有人动态的生成Spring的配置文件。下面是我这两个需求的实现
我的思路是:将SimpleMailMessage替换成能够添加附件的类MimeMessage
并且手动初始化JavaMailSenderImpl和MimeMessage,这样就可以用自定义配置文件来实现动态设置email属性。
1、上述Spring配置中的
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="邮件服务器地址"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
</props>
</property>
</bean>
<!—配置邮件属性-->
<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="to" value="目标地址"/>
<property name="from" >
<list>
<value></value>
</list>
</property>
<property name="subject" value="test"/>
<property name="subject" value="test"/>
</bean>
删掉或注释掉
2、 修改发送类MailService
public class MailService {
private MailSender mailSender;
private SimpleMailMessage mailMessage;//邮件模板
public MailService (){
Properties prop = new Properties();
InputStream is = ReportServiceImpl.class.getClassLoader()
.getResourceAsStream("reportset.properties");
try {
prop.load(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
String host = prop.getProperty("host");
String userName = prop.getProperty("userName");
String pwd = prop.getProperty("password");
String from = prop.getProperty("fromEmail");
String to = prop.getProperty("toEmail");
Date date = new Date();
String subject = date.getMonth() + "-" + date.getYear() + " " +
prop.getProperty("subject");
String reportPath = prop.getProperty("reportPath");
file = new FileSystemResource(reportPath);
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(host);
mailSender.setUsername(userName);
mailSender.setPassword(pwd);
Properties p = new Properties();
p.setProperty("mail.smtp.auth", "true");
mailSender.setJavaMailProperties(p);
MimeMessage mm = mailSender.createMimeMessage();
MimeMessageHelper mmh;
try {
mmh = new MimeMessageHelper(mm,
MimeMessageHelper.MULTIPART_MODE_MIXED,
"utf-8");
mmh.setFrom(from);
mmh.setTo(to.split(";"));
mmh.setSubject(subject);
mmh.addAttachment(file.getFilename(), file.getFile());
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendMail() {
System.out.println("-----------发送邮件!---------");
try {
mailSender.send(mailMessage);
} catch (MailException e) {
e.printStackTrace();
}
System.out.println("-----------发送成功!---------");
} //省略set,get方法}
3、 配置文件reportset.properties,放在工程的src下
host=www.iesgroup.com.cn
userName=liu.yijie
password=lyj2009
fromEmail=liu.yijie
toEmail=liu.yijie@iesgroup.com.cn
subject=Asset monthly report
reportPath=C://devices.xls
至此,我们就可以定时发送带附件的邮件了发送