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 首先 ...
随机推荐
- 网站开发中的相对URL问题--JSP
问题描述: 入门网站开发时,我们会在相对URL问题上有疑惑.例如,在一个jsp页面中引入css外部文件, <link rel="stylesheet" hr ...
- poj 2195 Going Home(最小费用最大流)
题目:http://poj.org/problem?id=2195 有若干个人和若干个房子在一个给定网格中,每人走一个都要一定花费,每个房子只能容纳一人,现要求让所有人进入房子,且总花费最小. 构造一 ...
- bzoj1485
首先考虑dp,设f[i,j]表示1~i用过了,期中j个放在偶数位然后转移大家都会 这显然TLE,我们观察这个dp,任意前i个数,无论怎么放,放在奇数位的数的个数一定要大于等于放在偶数位的个数 于是很明 ...
- UVa 1213 (01背包变形) Sum of Different Primes
题意: 选择K个质数使它们的和为N,求总的方案数. 分析: 虽然知道推出来了转移方程, 但还是没把代码敲出来,可能基本功还是不够吧. d(i, j)表示i个素数的和为j的方案数,则 d(i, j) = ...
- HAOI2007反素数
1053: [HAOI2007]反素数ant Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1346 Solved: 732[Submit][Sta ...
- Spark(1) - Getting Started with Apache Spark
Introduction Apache Spark is a general-purpose cluster computing system to process big data workload ...
- acdream 小晴天老师系列——晴天的后花园 (暴力+剪枝)
小晴天老师系列——晴天的后花园 Time Limit: 10000/5000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) ...
- Java [Leetcode 292]Nim Game
问题描述: You are playing the following Nim Game with your friend: There is a heap of stones on the tabl ...
- Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge
题意:一个无向图联通中,求包含每条边的最小生成树的值(无自环,无重边) 分析:求出这个图的最小生成树,用最小生成树上的边建图 对于每条边,不外乎两种情况 1:该边就是最小生成树上的边,那么答案显然 2 ...
- Mobile testing基础之Native、Web、Hybrid、activity、webview
应用一词指的是app,即application.原生应用指的是能直接运行于当前操作系统的应用程序:web应用指需要在浏览器中运行的网页应用,由于界面体验.功能上都更加强大,可媲美原生应用,故称web应 ...