标签:
=============JAVA后台代码=====================
package com.qgc.service.autoSendMsg.AutoSendMsg
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import com.gzbugu.common.service.impl.BaseService;
import com.gzbugu.common.util.JxlsUtils;
import com.gzbugu.dzz.domain.DzzUserinfo;
/**
* 自动导出数据,并发送邮件个用户
* @author http://cnblogs.com/qgc88
*
*/
public class AutoSendMsg {
private String host = ""; //smtp服务器
private String from = ""; //发件人地址
private String to = ""; //收件人地址
private String affix = ""; //附件地址
private String affixName = ""; //附件名称
private String user = ""; //用户名
private String pwd = ""; //密码
private String subject = ""; //邮件标题
private String count="";//文件内容
public void sendEmail() {
try {
String hql = " from DzzUserinfo o where enabled =1 ";
List<DzzUserinfo> list = commonDao.getListByHQL(hql);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HHmmss");
String bathPath = this.getClass().getClassLoader().getResource("")
.getPath();
//bathPath=bathPath.substring(1,bathPath.length());
bathPath=bathPath.substring(1,bathPath.lastIndexOf("WEB-INF"))+"dzz"+"/newOrderData/";
File file=new File(bathPath);
if(!file.exists()){
file.mkdirs();
}
String flileName=sdf.format(new Date())+".xls";
String tempPath =bathPath+flileName;
String template =bathPath+"newOrder_template.xls";
// 采用jxls模板方式导出
Map params = new HashMap();
params.put("list", list);
JxlsUtils.createExcel(template, params, tempPath);
System.out.println(tempPath);
//设置发件人地址、收件人地址和邮件标题
setAddress("发送人邮箱@qq.com","收件人邮箱@qq.com",flileName+",大中专学生最新报订数据!");
//设置要发送附件的位置和标题
setAffix(tempPath,flileName);
count="大中专学生最新报订数据,详情请查收附件,谢谢!";
//设置smtp服务器以及邮箱的帐号和密码
send("发送人邮箱@qq.com","发送人邮箱密码");
} catch (Exception e) {
e.printStackTrace();
}
}
public void send(String user,String pwd) {
this.user = user;
this.pwd = pwd;
this.host = "smtp."
+ from.substring(from.indexOf(‘@‘) + 1, from.length()); // 在Internet上发送
Properties props = new Properties();
//设置发送邮件的邮件服务器的属性(这里使用网易的smtp服务器)
props.put("mail.smtp.host", host);
//需要经过授权,也就是有户名和密码的校验,这样才能通过验证(一定要有这一条)
props.put("mail.smtp.auth", "true");
//用刚刚设置好的props对象构建一个session
Session session = Session.getDefaultInstance(props);
//有了这句便可以在发送邮件的过程中在console处显示过程信息,供调试使
//用(你可以在控制台(console)上看到发送邮件的过程)
session.setDebug(true);
//用session为参数定义消息对象
MimeMessage message = new MimeMessage(session);
try{
//加载发件人地址
message.setFrom(new InternetAddress(from));
//加载收件人地址
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
//加载标题
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setText(count);
multipart.addBodyPart(contentPart);
//添加附件
BodyPart messageBodyPart= new MimeBodyPart();
DataSource source = new FileDataSource(affix);
//添加附件的内容
messageBodyPart.setDataHandler(new DataHandler(source));
//添加附件的标题
//这里很重要,通过下面的Base64编码的转换可以保证你的中文附件标题名在发送时不会变成乱码
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
messageBodyPart.setFileName("=?GBK?B?"+enc.encode(affixName.getBytes())+"?=");
multipart.addBodyPart(messageBodyPart);
//将multipart对象放到message中
message.setContent(multipart);
//保存邮件
message.saveChanges();
// 发送邮件
Transport transport = session.getTransport("smtp");
//连接服务器的邮箱
transport.connect(host, user, pwd);
//把邮件发送出去
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void setAddress(String from,String to,String subject){
this.from = from;
this.to = to;
this.subject = subject;
}
public void setAffix(String affix,String affixName){
this.affix = affix;
this.affixName = affixName;
}
}
===============application.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-autowire="byName" default-lazy-init="true">
<!--一个普通对象-->
<bean id="autoSendMsg" class="com.qgc.service.autoSendMsg.AutoSendMsg"/>
<bean id="autoSendMsgTaskInit" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!--设置为定时调用的对象-->
<property name="targetObject">
<ref local="autoSendMsg" />
</property>
<!--设置对象里某一个方法为定时工作的方法-->
<property name="targetMethod">
<value>sendEmail</value>
</property>
</bean>
<bean id="autoSendMsgTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<!--把定时对象加入定时器-->
<property name="jobDetail">
<ref local="autoSendMsgTaskInit"/>
</property>
<!--设置时间的调用规制-->
<property name="cronExpression">
<!--0 15 10 ? * * 每天23点140分触发 -->
<value>0 40 23 ? * * </value>
</property>
</bean>
<bean id="autoSendMsgScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<!--把定时器加入管理器-->
<property name="triggers">
<list>
<ref bean="autoSendMsgTrigger"/>
</list>
</property>
</bean>
</beans>
===================newOrder_template.xls导出模版==================================
java使用jxl,自动导出数据excle,quartz自动发送邮件
标签:
原文地址:http://www.cnblogs.com/qgc88/p/4587370.html