文件配置例如以下:

<?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定期发送邮件的更多相关文章

  1. Spring Quartz 和 Spring Task使用比较

    Quartz 和 Spring Task执行时间对比: 1. Quartz同步模式:一个任务的两次执行的时间间隔是:“执行时间”和“trigger的设定间隔”的最大值 2. Task默认同步模式:一个 ...

  2. Spring 定时器 定时访问数据库并发送邮件

    我这里有两个案例的方法: 第一种:使用Spring quartz: 我这里使用的jar:spring-context-support.jar.quartz-1.6.5.jar ============ ...

  3. quartz与spring进行集成

    上一篇将了quartz框架的使用,spring同样也提供了对quartz的集成.这次就尝试一下在spring中集成quartz. 要在spring中使用job,Trigger和Scheduler,就要 ...

  4. atititt.java定时任务框架选型Spring Quartz 注解总结

    atititt.java定时任务框架选型Spring Quartz 总结 1. .Spring Quartz  (ati recomm) 1 2. Spring Quartz具体配置 2 2.1. 增 ...

  5. SSH内存泄露及Spring Quartz问题

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://www.blogbus.com/anoxia-logs/34360203.html 问题的起因: 为客户开发了一个系统权 ...

  6. Quartz与Spring的整合使用

    之前说到过Quartz的基本使用(猛戳这里看文章).在实际使用中,我们一般会将定时任务交由spring容器来管理.所以今天我们来说说Quartz与spring的整合. 咱们还是依照Quartz的三大元 ...

  7. 使用Spring框架整合Java Mail

    我的博客名为黑客之谜,今天演示的案例中会出现我的邮箱,还不赶紧收藏!我现在是小白,但是随着时间的流逝,我会逐渐向大神走进,所以,喜欢我的,或者喜欢大神的,点一波关注吧!顺便说一下,双十二快到了,有什么 ...

  8. Quartz 在 Spring 中如何动态配置时间--转

    原文地址:http://www.iteye.com/topic/399980 在项目中有一个需求,需要灵活配置调度任务时间,并能自由启动或停止调度. 有关调度的实现我就第一就想到了Quartz这个开源 ...

  9. spring quartz定时任务

    配置quartz 在spring中需要三个jar包: quartz-1.8.5.jar.commons-collections-3.2.1.jar.commons-logging-1.1.jar 首先 ...

随机推荐

  1. bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    筛法. 枚举每个数,它会对它的倍数的答案有贡献. 数大了以后,倍数相应少了很多.比枚举每个数的约数要好的多. 自己yy了一种分步做法.小于sqrt(m)被当作约数枚举,大于sqrt(m)的枚举倍数. ...

  2. Flask

    #environ:一个包含所有HTTP请求信息的dict对象 #start_response:一个发送HTTP响应的函数 def application(environ, start_response ...

  3. service name和SID的区别

    数据库名(DB_NAME).实例名(Instance_name).以及操作系统环境变量(ORACLE_SID)  在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instan ...

  4. css页面重构面试题

    偶然间又看到博客园中这两道页面重构面试题.题一:分别用2个DIV,3个DIV,5个DIV实现水平垂直均居中显示一个宽50px,高200px的正十字架.思路:水平垂直均居中的实现,当然非absolute ...

  5. 【转】Linux(Ubuntu)下面SecureCRT 完全破解

    仅供测试, 勿用作商业用途.首先要到vandyke网站下载一个securecrt, 需要注册.http://www.vandyke.com/download/securecrt/download.ht ...

  6. 017QTP 描述性编程的使用方法

    一.什么时候使用描述性编程 在测试过程中,有些界面元素是动态出现或动态变化的,在录制的时候并没有添加到对象库中 二.描述性编程的运行原理 用描述性编程编写的测试脚本在运行时,QTP会使用测试脚本中给出 ...

  7. 黑盒测试用例设计方法&理论结合实际 -> 正交试验法

    一. 概念 依据Galois理论,从大量的(实验)数据(测试例)中挑选适量的,有代表性的点(例),从而合理地安排实验(测试)的一种科学实验设计方法.类似的方法有:聚类分析方法,因子方法方法等. 二. ...

  8. Msp430概述

    总结一下MSP430给我的印象吧,感觉他就是一个迷你型的arm 1:MSP430采用的是精简指令,他只有27条核心的汇编指令,这一点和arm相同,arm同样是采用精简指令,而80c51采用的是冗余指令 ...

  9. GIS中相交的定义(OGC相交的定义)

    我们常用GIS中的相交,比如在地图漫游的时候,屏幕显示的图形和屏幕这个包络线就是相交的关系.我们常用的GIS工具,拉框查询,这个用到的也是相交. 首先题目开起来很简单(开始的时候我是这样想的),但是做 ...

  10. springmvc 传递对象数组参数 property path is neither an array nor a List nor a Map

    Spring MVC 3: Property referenced in indexed property path is neither an array nor a List nor a Map ...