- 浏览: 1055226 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (279)
- Apache net (10)
- JBoss Rules (3)
- Seam ACL (1)
- Seam (14)
- Seam JPA高级权限验证 (8)
- 待解决的问题.... (1)
- JAVA (43)
- Dwr (4)
- Ajax4JSF (1)
- JavaScript (27)
- 生活小常识 (17)
- Richfaces (3)
- seam自己经历 (14)
- JDBC (1)
- mysql (6)
- WebService (10)
- Java Web (4)
- Hibernate (13)
- J2EE框架整合 (3)
- Spring (9)
- BEA Weblogic (1)
- XML (1)
- log4j (6)
- CSS (2)
- javaIO文件的读写 (5)
- SVN服务器的安装 (5)
- powerDesigner (2)
- SQL常用语句 (3)
- wicket初学 (5)
- eclipse (7)
- 正则表达式 (1)
- ExtJS (6)
- maven(m2eclipse) (1)
- struts2.0 (9)
- JPA (6)
- struts2.0整合spring2.5 (9)
- linux (6)
- Oracle (5)
- Servlet (3)
- MyEclipseGen (0)
最新评论
-
qq_31247573:
JAVA 获取http返回XML的数据 -
jasmine_20100810:
...
linux下tomcat服务的启动、关闭与错误跟踪 -
weiaiFang0624:
视频下载地址:http://download.csdn.net ...
there is no action mapped for namespace / and action name解决办法 -
p476462534:
JS控制表单form的提交 -
dandongsoft:
aaaaaaaaaaaaaaa
httpClient,JAVA访问http request response
一 发送邮件的基类:
- package com.spring.mail;
- import org.springframework.mail.MailSender;
- //import org.springframework.mail.javamail.JavaMailSender;
- public abstract class BaseMailSender {
- protected String to;
- protected String from;
- protected String subject;
- protected MailSender sender;
- //当发送的邮件为简单邮件时用MailSender即可;
- //protected JavaMailSender sender;
- //当发送的邮件为MIMI时用JavaMailSender
- 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 (MailSender sender) {//(JavaMailSender sender) {
- this.sender = sender;
- }
- }
package com.spring.mail; import org.springframework.mail.MailSender; //import org.springframework.mail.javamail.JavaMailSender; public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected MailSender sender; //当发送的邮件为简单邮件时用MailSender即可; //protected JavaMailSender sender; //当发送的邮件为MIMI时用JavaMailSender 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 (MailSender sender) {//(JavaMailSender sender) { this.sender = sender; } }
发送邮件调用的类:
- package com.spring.mail;
- import javax.mail.MessagingException;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.mail.SimpleMailMessage;
- public class SimpleHtmlMailSender extends BaseMailSender{
- public void sendMessage() throws MessagingException {
- String textStr = "Helo!!!!!!!!!!!";//发送邮件的消息主体
- SimpleMailMessage msg = new SimpleMailMessage();
- msg.setTo(to);
- msg.setFrom(from);
- msg.setSubject(subject);
- msg.setText(textStr);
- sender.send(msg);
- }
- public void doIt() throws Exception {
- ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext(
- new String[] { "ApplicationContext.xml" });
- SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx1.getBean("messageSender");
- sender.sendMessage();
- System.out.println("发送成功");
- }
- }
package com.spring.mail; import javax.mail.MessagingException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; public class SimpleHtmlMailSender extends BaseMailSender{ public void sendMessage() throws MessagingException { String textStr = "Helo!!!!!!!!!!!";//发送邮件的消息主体 SimpleMailMessage msg = new SimpleMailMessage(); msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); sender.send(msg); } public void doIt() throws Exception { ClassPathXmlApplicationContext ctx1 = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx1.getBean("messageSender"); sender.sendMessage(); System.out.println("发送成功"); } }
配置文件ApplicationContext:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
- <beans>
- <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
- <property name="host">
- <value>smtp.163.com</value>
- </property>
- <property name="username">
- <value>tomcat0506</value>
- </property>
- <property name="password">
- <value>wes</value>
- </property>
- <property name="javaMailProperties">
- <props>
- <prop key="mail.smtp.auth">true</prop>
- </props>
- </property>
- </bean>
- <bean id="messageSender" class="com.spring.mail.SimpleHtmlMailSender">
- <property name="javaMailSender">
- <ref bean="sender"/>
- </property>
- <property name="to">
- <value>wes830506@126.com</value>
- </property>
- <property name="from">
- <value>tomcat0506@163.com</value>
- </property>
- <property name="subject">
- <value>test</value>
- </property>
- </bean>
- <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <!-- <property name="jobClass">
- <value>com.spring.mail.SimpleHtmlMailSender</value>
- </property>-->
- <property name="targetObject"><ref bean="messageSender"/></property>
- <property name="targetMethod"><value>doIt</value></property>
- </bean>
- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property>
- <property name="startDelay">
- <!-- 3 seconds -->
- <value>3000</value>
- </property>
- <property name="repeatInterval">
- <!-- repeat every 6 seconds -->
- <value>6000</value>
- </property>
- </bean>
- <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail"> <ref bean="methodInvokingJobDetail"/></property>
- <property name="cronExpression"> <value>0 0 6,12,20 * * ?</value> </property>
- </bean>
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref local="simpleTrigger"/>
- </list>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd "> <beans> <bean id="sender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host"> <value>smtp.163.com</value> </property> <property name="username"> <value>tomcat0506</value> </property> <property name="password"> <value>wes</value> </property> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">true</prop> </props> </property> </bean> <bean id="messageSender" class="com.spring.mail.SimpleHtmlMailSender"> <property name="javaMailSender"> <ref bean="sender"/> </property> <property name="to"> <value>wes830506@126.com</value> </property> <property name="from"> <value>tomcat0506@163.com</value> </property> <property name="subject"> <value>test</value> </property> </bean> <bean id="methodInvokingJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- <property name="jobClass"> <value>com.spring.mail.SimpleHtmlMailSender</value> </property>--> <property name="targetObject"><ref bean="messageSender"/></property> <property name="targetMethod"><value>doIt</value></property> </bean> <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="jobDetail"><ref bean="methodInvokingJobDetail"/></property> <property name="startDelay"> <!-- 3 seconds --> <value>3000</value> </property> <property name="repeatInterval"> <!-- repeat every 6 seconds --> <value>6000</value> </property> </bean> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail"> <ref bean="methodInvokingJobDetail"/></property> <property name="cronExpression"> <value>0 0 6,12,20 * * ?</value> </property> </bean> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref local="simpleTrigger"/> </list> </property> </bean> </beans>
二 mimi邮件发送
在编写发送邮件程序时需要用到的类有一下几个:
org.springframework.mail.javamail.JavaMailSender;
//此类继承了spring里面的
//MailSender接口--定义了两个简单的sender方法,用于发送简单的邮件信息,参数类型为:SimpleMailMessage([])
//JavaMailSender类继承了上面方法后支持MIMI邮件,可以发MIMI消息
org.springframework.context.support.ClassPathXmlApplicationContext;
//spring里面读取配置文件时需要用到的类
org.springframework.mail.javamail.MimeMessageHelper;
//在发送MIMI消息时需要用到的辅助类
javax.mail.MessagingException;
javax.mail.internet.MimeMessage;
//J2EE里面支持发送MIMe信息需要的类
例子:
发送邮件的基类
BaseMailSender .java:
- import org.springframework.mail.javamail.JavaMailSender;
- public abstract class BaseMailSender {
- protected String to;
- protected String from;
- protected String subject;
- protected JavaMailSender sender;//当发送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(JavaMailSender sender) {
- this.sender = sender;
- }
- }
import org.springframework.mail.javamail.JavaMailSender; public abstract class BaseMailSender { protected String to; protected String from; protected String subject; protected JavaMailSender sender;//当发送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(JavaMailSender sender) { this.sender = sender; } }
发送邮件的类:
- package com.spring.mail;
- import javax.mail.MessagingException;
- import javax.mail.internet.MimeMessage;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.mail.SimpleMailMessage;
- import org.springframework.mail.javamail.MimeMessageHelper;
- public class SimpleHtmlMailSender extends BaseMailSender{
- public void sendMessage() throws MessagingException {
- //String htmlHead = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>";
- //String htmlBody = "";
- //String htmlEnd = "";
- // StringBuffer mailMessage = new StringBuffer();
- //mailMessage.append(htmlHead);
- //mailMessage.append(htmlBody);
- //mailMessage.append(htmlEnd);
- //当发送的邮件为简单邮件时注销此句
- String textStr = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>^_^!</a></h1></body></html>";
- //String textStr = "Helo could you see this email?";//发送邮件的消息主体
- //SimpleMailMessage msg = new SimpleMailMessage();
- //当发送邮件为简单邮件时把此句注销即可
- MimeMessage msg = sender.createMimeMessage();
- MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312");
- helper.setTo(to);
- helper.setFrom(from);
- helper.setSubject(subject);
- helper.setText(textStr, true);
- /*
- msg.setTo(to);
- msg.setFrom(from);
- msg.setSubject(subject);
- msg.setText(textStr);
- */
- sender.send(msg);
- }
- public void doIt() throws Exception{//static void main(String[] args) throws Exception {//
- //ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "springexample-creditaccount.xml" });
- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
- new String[] { "ApplicationContext.xml" });
- SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx
- .getBean("messageSender");
- sender.sendMessage();
- System.out.println("发送成功");
- }
- }
package com.spring.mail; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.MimeMessageHelper; public class SimpleHtmlMailSender extends BaseMailSender{ public void sendMessage() throws MessagingException { //String htmlHead = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body>"; //String htmlBody = ""; //String htmlEnd = ""; // StringBuffer mailMessage = new StringBuffer(); //mailMessage.append(htmlHead); //mailMessage.append(htmlBody); //mailMessage.append(htmlEnd); //当发送的邮件为简单邮件时注销此句 String textStr = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"></head><body><h1><a href='#'>^_^!</a></h1></body></html>"; //String textStr = "Helo could you see this email?";//发送邮件的消息主体 //SimpleMailMessage msg = new SimpleMailMessage(); //当发送邮件为简单邮件时把此句注销即可 MimeMessage msg = sender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(msg, true, "GB2312"); helper.setTo(to); helper.setFrom(from); helper.setSubject(subject); helper.setText(textStr, true); /* msg.setTo(to); msg.setFrom(from); msg.setSubject(subject); msg.setText(textStr); */ sender.send(msg); } public void doIt() throws Exception{//static void main(String[] args) throws Exception {// //ApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] { "springexample-creditaccount.xml" }); ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "ApplicationContext.xml" }); SimpleHtmlMailSender sender = (SimpleHtmlMailSender) ctx .getBean("messageSender"); sender.sendMessage(); System.out.println("发送成功"); } }
配置文件同上!
发表评论
-
看看Spring的contextLoader都做了什么
2010-06-21 22:01 10649public class ContextLoader { ... -
寻找spring框架的启动入口 ContextLoaderListener
2010-06-21 21:58 12376spring在web下的入口在配置文件web.xml的监听器中 ... -
User Role userRole
2010-02-23 11:38 1797条件说明:在用户注册时候,选择role,然后再插入到数据库表中 ... -
spring的applicationContext.xml中使用属性文件*.properties
2009-08-21 15:46 1836我们可以生成如下的属性文件(/WEB-INF/jdbc.pro ... -
怎样使用Spring发邮件?(转)
2009-08-21 15:35 1628关于Spring的邮件发送的功能演示: (我把程序中一些敏感 ... -
利用Spring发送邮件
2009-08-21 15:34 1530本文主要介绍如何使用 ... -
Spring的MVC framework
2009-08-21 15:23 1401下面列举一下Spring的MV ... -
Spring XML配置的12个技巧
2009-08-21 15:17 1097Spring是一个强有力的java程序框架,其被广泛应用于 ...
相关推荐
Spring整合Quartz定时发送邮件涉及到的关键技术点有: 1. 使用Apache Commons Email库进行邮件发送。 2. 创建Quartz Job,继承Spring提供的`QuartzJobBean`,并在`executeInternal()`方法中执行实际任务。 3. 在...
在IT行业中,定时任务是...通过这样的整合,你可以实现一个灵活且可扩展的定时邮件发送系统,不仅可以定期向用户发送通知,也可以在特定事件触发时发送邮件。这在很多业务场景中都非常实用,如定期报告、订阅提醒等。
总之,结合Spring Boot、Quartz和邮件服务,我们可以构建出强大的定时任务系统,自动从数据库提取数据,生成报表,并将结果以邮件的形式发送给指定的接收者。这样的系统在许多业务场景中都有广泛的应用,例如日常...
通过上述步骤,我们不仅了解了如何在Spring环境中结合JavaMail和Quartz实现定时邮件发送,而且还深入探索了Spring框架如何简化JavaMail API的使用,以及Quartz如何提供灵活的定时任务调度能力。这种技术组合为Web...
在 "spring+quartz定时小例子" 中,我们主要关注的是如何将这两个框架结合使用,创建一个可以执行定时任务的应用。这个例子包含了相关的框架包和文档,帮助开发者理解并实践这一整合过程。 1. **Spring 配置**: ...
要实现定时发送邮件功能,需要使用Spring框架和Quartz框架结合。首先,需要在ApplicationContext.xml文件中配置Quartz的Bean,包括JobDetail和Trigger。JobDetail用于定义要执行的任务,而Trigger用于定义任务的执行...
Spring提供了一种集成Quartz的方式,使得我们可以方便地在Spring应用中管理和执行定时任务。本篇将深入探讨如何在Spring中启动和停止Quartz定时器。 首先,我们需要理解Spring和Quartz的基本概念。Spring是一个强大...
而Quartz是一款强大的开源作业调度框架,可以用于在特定时间执行任务,比如定时发送邮件、数据同步等。 在Java Spring中整合Quartz,可以利用Spring的管理能力来控制Quartz的作业和触发器,使得整个系统更加灵活和...
Spring Quartz常用于执行后台维护任务,例如数据清理、统计报告生成、定时邮件发送等。通过灵活的触发器设置,可以实现各种定时策略,满足不同业务需求。 综上所述,Spring Quartz定时作业项目提供了一个完整的...
在IT行业中,定时任务是常见的一种需求,它允许程序在特定时间或按照一定周期自动执行某些操作,比如发送邮件、进行数据统计、更新系统状态、推送消息、开启活动或创建增量索引等。目前,主要有两种方式可以实现定时...
在实际开发中,Spring+Quartz的组合常常用于实现后台的定时任务,比如数据同步、报表生成、邮件发送等。通过这种方式,开发者可以把注意力集中在任务本身,而无需关心调度的细节,提高了代码的可维护性和复用性。 ...
在Java应用开发中,定时任务是一项常用的功能,用于执行周期性的、计划性的工作,例如数据同步、日志清理、发送邮件等。Spring框架提供了与Quartz库集成的能力,使得我们可以方便地创建和管理定时任务。 Spring与...
Quartz 主要用于解决需要定期执行的任务,如网页游戏中的定时事件、企业的日常数据同步、邮件发送等自动化操作。在网页游戏中,例如,可以设定角色自动修炼、道具效果计时等。在企业环境中,它可能用于每天固定时间...
关于压缩包中的文件"spring_扩展_邮件及定时任务_2",它很可能包含了示例代码或者配置文件,用于演示如何在Spring项目中实现邮件发送和定时任务。这些文件可能包括了Java源代码、XML配置文件或者YAML配置文件,你...
Quartz则是用于创建和管理定时任务的开源库,它允许程序根据预定义的规则执行任务,如定时发送邮件、数据同步等。 集成Spring和Quartz,我们需要以下几个步骤: 1. **引入依赖**:在项目中添加Spring和Quartz的...
在Spring框架中,定时任务是常见的需求,例如用于执行定期数据同步、日志清理或发送邮件等。Quartz是一款强大的开源任务调度库,它能够与Java应用程序无缝集成,实现复杂的时间触发任务。当我们遇到Spring定时器执行...
总的来说,Spring 和 Quartz 的集成是一项常见的需求,它使得开发者能够轻松地在应用中实现定时任务,如定期备份数据库、发送邮件等。了解并掌握这种集成方法对于提升 Java 后端应用的自动化处理能力至关重要。
描述中提到"应对不同版本的spring 与quartz定时任务创建所需要的不同的jar包",这意味着此压缩包中包含的 jar 文件可以用于配置和实现基于 Spring 的 Quartz 定时任务。在 Spring 中,Quartz 可以被集成用来执行后台...