StevenBot-Saltsam

眼睛能装下世界,为何却装不下眼泪? 一只风筝一辈子只为一根线冒险。 那不是一场游戏,为何总有一根线牵着心怀,隐隐作疼? 那不是一段邂逅,为何飘在桥上的影子,总缠进梦乡? 那不是一个梦境,为何你的温柔私语,总是不经意的响起?

导航

<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

datas link

OSChinal Sources codes Library

搜索

最新评论

阅读排行榜

评论排行榜

使用Spring和Quartz进行定时邮件发送

关于Spring的邮件发送的功能演示:(下面是我自己写的一个可以发送简单邮件和附件的邮件功能,可以改换邮箱后直接使用)? 需要用到的jar包是
? spring-context.jar
? mail-1.4.jar
? spring-core.jar
? activation.jar
? commons-logging.jar
? 这些是程序必须要用到的包。1.BaseMailSender.javapackage com.trisun.T82A.java.mail;
import org.springframework.mail.javamail.JavaMailSender;
public abstract class BaseMailSender {
?protected String to;
?protected String from;
?protected String subject;
?protected JavaMailSendersender;//当发送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(JavaMailSendersender) {
? this.sender = sender;}2.SimpleHtmlMailSender.javapackage com.trisun.T82A.java.mail;
import java.io.File;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.MimeMessageHelper;public class SimpleHtmlMailSender extendsBaseMailSender{?public? void sendMessage()throws MessagingException {????? String textStr ="<html><head><metahttp-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"></head><body><h1><ahref='#'>^_^!</a></h1></body></html>";? //String textStr = "Helo could you see thisemail?";//发送邮件的消息主体? //SimpleMailMessage msg = newSimpleMailMessage();? //当发送邮件为简单邮件时把此句注销即可? MimeMessage msg =sender.createMimeMessage();
? MimeMessageHelper helper = newMimeMessageHelper(msg, true, "utf-8");? helper.setTo(to);
? helper.setFrom(from);
? helper.setSubject(subject);
?// helper.addAttachment("a.doc", newClassPathResource("/a.doc"));
? FileSystemResource file = newFileSystemResource(newFile("d:/mail/b.rar"));??? helper.addAttachment("b.rar",file);
? helper.setText(textStr, true);
? sender.send(msg);
?}?}3.SpringSendMail.javapackage com.trisun.T82A.java.mail;
import java.util.TimerTask;importorg.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSenderImpl;?public class SpringSendMail{
????public SimpleHtmlMailSender messageSender;
?public SimpleHtmlMailSender getMessageSender(){
??return messageSender;
?}?public voidsetMessageSender(SimpleHtmlMailSender messageSender) {
??this.messageSender =messageSender;
?}?public SpringSendMail() {}? private JavaMailSenderImpl mailSender;? public JavaMailSenderImpl getMailSender(){
????return mailSender;
? }? public voidsetMailSender(JavaMailSenderImpl mailSender) {
????this.mailSender = mailSender;? public void execute() {????try{??
??????messageSender.sendMessage();
????System.out.println("发送成功");????}catch(Exception e){
?????e.printStackTrace();? }?}
4.applicationContext-mail.xml<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
?default-lazy-init="true">?<bean id="sender"
??class="org.springframework.mail.javamail.JavaMailSenderImpl">
??<propertyname="host">
???<value>smtp服务器地址</value>
??</property>
??<propertyname="username">
???<value>user</value>
??</property>
??<propertyname="password">
???<value>pwd</value>
??</property>
??<propertyname="javaMailProperties">
???<props>
????<propkey="mail.smtp.auth">true</prop>
???</props>
??</property>
?</bean>
?<bean id="messageSender"
??class="com.trisun.T82A.java.mail.SimpleHtmlMailSender">
??<propertyname="javaMailSender">
???<refbean="sender" />
??</property>
??<propertyname="to">
???<value>64256975@qq.com</value>
??</property>
??<propertyname="from">
???<value>t8log@jhsys.cn</value>
??</property>
??<propertyname="subject">
???<value>test</value>
??</property>
?</bean></beans>
5.applicationContext-quartz.xml<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
?xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
?default-lazy-init="false">?<bean id="springSendMail"
??class="com.trisun.T82A.java.mail.SpringSendMail">
??<propertyname="messageSender" ref="messageSender" />
?</bean>
??<beanid="springSendMailDetail"
??class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
??<propertyname="concurrent" value="true" />
??<propertyname="targetObject" ref="springSendMail" />
??<propertyname="targetMethod" value="execute" />
?</bean>
?<!--?
??Seconds?YES? 0-59? , - */?
??Minutes?YES? 0-59? , - */?
??Hours?YES? 0-23? , - */?
??Day of month?YES? 1-31? , - * ? / L W
??Month?YES? 1-12 or JAN-DEC? , - */?
??Day of week?YES? 1-7 or SUN-SAT? , - * ? / L#?
??Year?NO? empty, 1970-2099? , - */?
?-->?<beanid="springSendMailDetailTask"
??class="org.springframework.scheduling.quartz.CronTriggerBean">
??<propertyname="jobDetail" ref="springSendMailDetail" />
??<!-- 0 * * * * ?*???每分钟-->
??<!-- 0 0 0 * * ?*???每天-->
??<!-- 0 0 0 1 * ?*???每月-->
??<propertyname="cronExpression" value="0/3 * * * * ?*"></property>
?</bean>
?<bean id="QuartzJobFactory"
??class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
??<propertyname="triggers">
???<list>
????<refbean="springSendMailDetailTask" />
???</list>
??</property>
??<!--设置是否Spring容器初始化后马上启动Scheduler,默认为true。如果设置为false则需要手工启动Scheduler-->
??<propertyname="autoStartup" value="true" />
?</bean></beans>秒 是 0-59 , - * /
分 是 0-59 , - * /
时 是 0-23 , - * /
日 是 1-31 , - * ? / L W C
月 是 1-12 或 JAN-DEC , - * /
周 是 1-7 或 SUN-SAT , - * ? / L C #
年 否 空 或 1970-2099 , - * /

posted on 2011-10-15 09:55 Steven_bot 阅读(407) 评论(0)  编辑  收藏 所属分类: spring


只有注册用户登录后才能发表评论。


网站导航: