首页 > 编程语言 > 详细

Spring Quartz结合Spring mail定期发送邮件

时间:2014-06-10 08:52:53      阅读:302      评论:0      收藏:0      [点我收藏+]

文件配置例如以下:

<?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定期发送邮件,布布扣,bubuko.com

Spring Quartz结合Spring mail定期发送邮件

原文:http://www.cnblogs.com/blfshiye/p/3778610.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!