文件配置例如以下:

<?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. Zookeeper运维经验

    转自:http://www.juvenxu.com/2015/03/20/experiences-on-zookeeper-ops/ ZooKeeper 是分布式环境下非常重要的一个中间件,可以完成动 ...

  2. DataView.RowFilter筛选DataTable中的数据

    //定义一个DataView ,得到一个全部职员的视图DataView dataView1 = DbHelperSQL.QueryDataView(sql); //过滤得到一个只显示男职员的视图 da ...

  3. 如何从Linux源码获知版本信息

    /*************************************************************************** * 如何从Linux源码获知版本信息 * 声明 ...

  4. 【转】FTS抓包看蓝牙的SDP整个过程

    原文网址:http://blog.sina.com.cn/s/blog_69b5d2a50101f23c.html 1.概述   SDP是蓝牙的Service Discovery Protocol,用 ...

  5. jquery M97-datepicker日历控件

    My97DatePicker是一款非常灵活好用的日期控件.使用非常简单. 1.下载My97DatePicker组件包 2.在页面中引入该组件js文件:     <script type=&quo ...

  6. 怎么找到MyEclipse->add struts capabilities

    问:MyEclipse:我的工程右键可以MyEclipse->add struts capabilities之前有,后来不知怎么就没有了,谁知道怎么弄出来是struts 答:你已经add过一次了 ...

  7. (五)学习CSS之line-height属性

    参考:http://www.jb51.net/w3school/css/pr_dim_line-height.htm line-height 属性设置行间的距离(行高). 注释:不允许使用负值. 值 ...

  8. AngularJS with MVC4 CRUD

    CRUD using MVC Web API and AngularJS In this article I am going to demonstrate about how can we crea ...

  9. 在ASP.NET MVC中使用DropDownList

    在ASP.NET MVC中,尽管我们可以直接在页面中编写HTML控件,并绑定控件的属性,但更方便的办法还是使用HtmlHelper中的辅助方法.在View中,包含一个类型为HtmlHelper的属性H ...

  10. android ListView详解

    转自:http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html 在android开发中ListView是比较常用的组件,它以列表的形式展 ...