Spring Quartz结合Spring mail定期发送邮件
文件配置例如以下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:mail.properties" />
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" >
<value>${host}</value>
</property>
<property name="username" >
<value>${username}</value>
</property>
<property name="password" >
<value>${password}</value>
</property>
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.timeout">25000</prop>
</props>
</property>
</bean>
<import resource="spring-quartz2.xml"/>
<context:component-scan base-package="com.study"/>
</beans>
spring-quartz2.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" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd " >
<task:annotation-driven/>
</beans>
package com.study; import java.io.File; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
@Component
public class QuartzJob{
@Autowired
private JavaMailSender jms;
private SimpleMailMessage smm;
private MimeMessage mailMsg; public QuartzJob() throws ServletException{
//initSimpleMailMSG();
//initHTMLMailMSG();
initHTMLWithAttachMailMsg();
System.out.println("Quartzjob创建成功");
}
@Scheduled(cron = "0/1 * * * * ? ")
public void run(){
System.out.println("Quartz运行的任务调度发送邮件");
try {
//jms.send(smm);
jms.send(mailMsg);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initSimpleMailMSG(){//发送简单邮件
smm = new SimpleMailMessage();
smm.setTo("253503125@qq.com");
smm.setFrom("hbzhongqian@163.com");
smm.setSubject("測试邮件");
smm.setText("springMail的简单測试发送邮件");
}
private void initHTMLMailMSG(){//发送HTML格式的邮件
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
mailMsg = senderImpl.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg,true,"utf-8");
messageHelper.setTo("253503125@qq.com");//接受者
messageHelper.setFrom("hbzhongqian@163.com");//发送者
messageHelper.setSubject("測试邮件");//主题
//邮件内容,注意加參数true,表示启用html格式
messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1><font color='red'>BaBY</font></body></html>",true); } catch (Exception e) {
e.printStackTrace();
}
}
private void initHTMLWithAttachMailMsg(){//发送带附件的邮件
JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
mailMsg = senderImpl.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(mailMsg,true,"utf-8");
messageHelper.setTo("253503125@qq.com");//接受者
messageHelper.setFrom("hbzhongqian@163.com");//发送者
messageHelper.setSubject("測试邮件");//主题
messageHelper.setText("<html><head></head><body><h1>hello!!chao.wang</h1></body></html>",true);
//附件内容
messageHelper.addInline("a", new File("E:/xiezi.png"));
// messageHelper.addInline("b", new File("E:/logo.png"));
// 这里的方法调用和插入图片是不同的,使用MimeUtility.encodeWord()来解决附件名称的中文问题
// messageHelper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
} catch (Exception e) {
e.printStackTrace();
}
} }
邮件发送带附件存在问题。
Spring Quartz结合Spring mail定期发送邮件的更多相关文章
- Spring Quartz 和 Spring Task使用比较
Quartz 和 Spring Task执行时间对比: 1. Quartz同步模式:一个任务的两次执行的时间间隔是:“执行时间”和“trigger的设定间隔”的最大值 2. Task默认同步模式:一个 ...
- Spring 定时器 定时访问数据库并发送邮件
我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ============ ...
- quartz与spring进行集成
上一篇将了quartz框架的使用,spring同样也提供了对quartz的集成.这次就尝试一下在spring中集成quartz. 要在spring中使用job,Trigger和Scheduler,就要 ...
- atititt.java定时任务框架选型Spring Quartz 注解总结
atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...
- SSH内存泄露及Spring Quartz问题
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://www.blogbus.com/anoxia-logs/34360203.html 问题的起因: 为客户开发了一个系统权 ...
- Quartz与Spring的整合使用
之前说到过Quartz的基本使用(猛戳这里看文章).在实际使用中,我们一般会将定时任务交由spring容器来管理.所以今天我们来说说Quartz与spring的整合. 咱们还是依照Quartz的三大元 ...
- 使用Spring框架整合Java Mail
我的博客名为黑客之谜,今天演示的案例中会出现我的邮箱,还不赶紧收藏!我现在是小白,但是随着时间的流逝,我会逐渐向大神走进,所以,喜欢我的,或者喜欢大神的,点一波关注吧!顺便说一下,双十二快到了,有什么 ...
- Quartz 在 Spring 中如何动态配置时间--转
原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...
- spring quartz定时任务
配置quartz 在spring中需要三个jar包: quartz-1.8.5.jar.commons-collections-3.2.1.jar.commons-logging-1.1.jar 首先 ...
随机推荐
- C语言计算程序运行时间
#include<stdio.h>#include<stdlib.h> #include "time.h" int main( void ) { ...
- Hibernate数据库持久层框架
Hibernate是一种Java语言下的对象关系映射解决方案. 它是使用GNU宽通用公共许可证发行的自由.开源的软件.它为面向对象的领域模型到传统的关系型数据库的映射,提供了一个使用方便的框架.Hib ...
- Codeforces 374B - Inna and Nine
原题地址:http://codeforces.com/problemset/problem/374/B 这道题没什么难度,但是考场上就是没写对.Round #220彰显了它的逗比性质——这道题的“标算 ...
- NET SCADA软件简介
Scada是一款采用.Net WPF开发的免费组态软件,功能强大,支持服务器客户端方式运行.支持浏览器运行,软件非常小巧,免安装,支持Modbus和OPC通讯协议, 开放构架,支持C#.Net脚本,J ...
- UVa 11971 (概率) Polygon
题意: 有一根绳子,在上面随机选取k个切点,将其切成k+1段,求这些线段能够成k+1边形的概率. 分析: 要构成k+1边形,必须最长的线段小于其他k个线段之和才行. 紫书上给出了一种解法,但是感觉理解 ...
- I.MX6 android BatteryService jni hacking
/**************************************************************************** * I.MX6 android Batter ...
- 用slf4j+logback实现多功能日志解决方案 --- 转
大家都知道,slf4j是原来log4j的作者写的一个新的日志组件,意思是简单日志门面接口,可以跟其他日志组件配合使用,常用的配合是slf4j+logback,无论从功能上还是从性能上都较之log4j有 ...
- keybd_event跟SendMessage,PostMessage模拟键盘消息的区别 z
首先你会发现keybd_event函数中是没有窗口句柄作为参数的,好奇的你一定会觉得很奇怪,那是因为,keybd_event是全局模拟按键的,只对前台窗口(即当前的活动窗口)才可以,但是如果模拟的按键 ...
- 27、Service
1服务可以通过startservice的方法 开启.通过stopservice的方法 停止. 服务有一个特点: 只会一次onCreate()方法一旦被创建出来,以后oncreate() 就不会再被执行 ...
- Flash AIR14导出ipa到Mac上的iOS模拟器测试
没错!你没看错!俺这篇博客就是关于Flash AIR的! ----------------无聊的分割线------------------ 朋友最近学习Flash AIR for iOS开发,想找我帮 ...