spring整合javamail跟quartz实现定时发送邮件

1.spring 整合 javamail  ---applicationContext-javamail-beans.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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" 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.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
        http://www.springframework.org/schema/tx  
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">

 		<!--最好用properties文件来控制值  -->
	<bean id="mail" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <!-- SMTP发送邮件的服务器的IP和端口 -->  
        <property name="host" value="smtp.163.com"/>  
        <property name="port" value="25"/>  
          
        <!-- 登陆SMTP邮件发送服务器的用户名和密码 -->  
        <property name="username" value="[email protected]"/> 
        <property name="password" value="xxxxxx"/>  
          
        <!-- 获得邮件会话属性,验证登录邮件服务器是否成功-->  
        <property name="javaMailProperties">  
             <props>  
                 <prop key="mail.smtp.auth">true</prop>  
                 <prop key="mail.smtp.timeout">25000</prop>  
             </props>  
        </property>  
	</bean>  
</beans>
</pre><pre name="code" class="html">
2.spring整合 quartz; <span style="font-family: Arial, Helvetica, sans-serif;">applicationContext-javamail-quartz.xml</span><span style="font-family: Arial, Helvetica, sans-serif;">	</span>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="white-space:pre">	</span>代码为:</span>
<pre name="code" class="html"><?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
<span style="white-space:pre">	</span><pre name="code" class="html"><span style="white-space:pre">	</span><bean id="taskExecutor"
		class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
		scope="singleton" lazy-init="true">
		<property name="corePoolSize" value="10" />   <!-- 并发线程数,想达到真正的并发效果,最好对应CPU的线程数及核心数 -->
		<property name="maxPoolSize" value="50" />   <!-- 最大线程池容量 -->
		<property name="queueCapacity" value="10000" />  <!-- 超过最大线程池容量后,允许的线程队列数 -->
<span style="white-space:pre">	</span></bean>

 
 
</pre><pre name="code" class="html">	<!-- 定时任务bean工厂 -->
	<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
			<!-- 每1分钟调javaMail发送邮件 -->
				<bean class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
						    <property name="jobDetail">
						    	<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
								    <property name="targetObject" ref="sendMailTask"/>
								    <property name="targetMethod" value="run"/>
								    <property name="concurrent" value="false"/>
								</bean>
						    </property>
						    <property name="cronExpression">
						         <value>0 0/1 * * * ?</value>
						    </property>
				</bean>
			</list>
		</property>
	</bean>
	<bean id="sendMailTask" class="com.hc360.logistics.thread.SendMailTask"/><!-- 调度类的实现 -->
</beans>
</pre>3.spring根文件中引入  以上两个xml文件
<span style="white-space:pre">	</span>代码为:<span style="font-family: Arial, Helvetica, sans-serif;">        </span>
<pre name="code" class="html"><import resource="applicationContext-javamail-beans.xml"/>
<import resource="applicationContext-javamail-quartz.xml"/>
</pre>4.实现定时器调度类(<span style="font-family: Arial, Helvetica, sans-serif;">com.hc360.logistics.thread.SendMailTask</span><span style="font-family: Arial, Helvetica, sans-serif;">)</span>
<span style="white-space:pre">	</span>代码为:
<pre name="code" class="java">package com.hc360.logistics.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.hc360.logistics.service.SendService;

/**
 * 定时发送邮件
 * @author mwang
 */
public class SendMailTask {
	private final static Logger log = LoggerFactory.getLogger(SendMailTask.class);
	@Autowired
	private SendService sendServiceImpl; 
	private void run() {
		sendServiceImpl.send();  //邮件发送方法
		log.info("发送邮件了...");
	}
}
</pre><pre name="code" class="html">5.一个获取JavaMailSender 的类 : MailSenderFactory.java
<span style="white-space:pre">	</span>代码为:
<pre name="code" class="java">package com.hc360.logistics.common.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
public class MailSenderFactory {  
    // 获取JavaMailSender bean   
    public static JavaMailSender getJavaMailSender() {  
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-javamail-beans.xml");  
        return (JavaMailSender) context.getBean("mail");  
    }  
}  
</pre>6.一个发送的接口(实现类为:文本的,html样式的,附件的(此处只写html的))(SendService)<span style="white-space:pre">	</span>代码为:
<pre name="code" class="java">package com.hc360.logistics.service;

public interface SendService {
	 public void send();  
}

7.实现类为:SendHtmlMailServiceImpl.java
 
 
<span style="white-space:pre">	</span>代码为:

package com.hc360.logistics.service.impl;
import java.util.Date;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import com.hc360.logistics.common.util.MailSenderFactory;
import com.hc360.logistics.service.SendService;

@Service
public class SendHtmlMailServiceImpl implements SendService {
	@Override
	public void send() {
		JavaMailSender mailSender = MailSenderFactory.getJavaMailSender();
		MimeMessage mimeMessage = mailSender.createMimeMessage();
		try {
			System.out.println("HTML脚本形式邮件正在发送...");
			// 设置utf-8或GBK编码,否则邮件会有乱码
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,
					"UTF-8");
			// 设置发送人名片
			helper.setFrom("[email protected]");
			// 设置收件人名片和地址
			helper.setTo(new InternetAddress("\""
					+ MimeUtility.encodeText("sina邮箱") + "\" <[email protected]>"));// 发送者
			// 邮件发送时间
			helper.setSentDate(new Date());
			// 设置回复地址
			helper.setReplyTo(new InternetAddress("[email protected]"));
			// 设置抄送的名片和地址
			helper.setCc(InternetAddress.parse(MimeUtility.encodeText("抄送人001")
					+ " <[email protected]>," + MimeUtility.encodeText("抄送人002")
					+ " <[email protected]>"));
			// 主题
			helper.setSubject("主题");
			// 邮件内容,注意加参数true,表示启用html格式
			helper.setText(
					"<html><head></head><body><h1>hello!!我是乔布斯</h1></body></html>",  //此处可以写html标签来拼装样式
					true);
			// 发送
			mailSender.send(mimeMessage);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("HTML脚本形式邮件发送成功!!!");
	}
}

8.遇见问题:
 
 
<span style="white-space:pre">	</span>geronimo-spec-javamail-1.3.1-rc3.jar  此包会出现包冲突问题,最好使用mail.jar
<span style="white-space:pre">	</span>
 
 

你可能感兴趣的:(spring整合javamail跟quartz实现定时发送邮件)