package day1.demo7;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
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 javax.mail.internet.MimeMessage.RecipientType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class SendMailExcle {
public static void readExcel(String excelFileName) throws BiffException, IOException{
// 创建一个list 用来存储读取的内容
//String excelFileName="D:/资料/Report_Day.xls";
Workbook rwb = null;
// 创建输入流
InputStream stream = new FileInputStream(excelFileName);
// 获取Excel文件对象
rwb = Workbook.getWorkbook(stream);
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
// 行数(表头的目录不需要,从1开始)
StringBuffer buffer = new StringBuffer();
//创建HTML
buffer.append("
"+sheet.getCell(0, 0).getContents()+" | |||||
---|---|---|---|---|---|
"+sheet.getCell(0, 1).getContents()+" | |||||
"+sheet.getCell(0, 2).getContents()+" | "+sheet.getCell(2, 2).getContents()+" | "+sheet.getCell(3, 2).getContents()+" | "+sheet.getCell(4, 2).getContents()+" | "+sheet.getCell(5, 2).getContents()+" | "+intTermString2+" | ";"+intTermString3+" | ";"+intTermString4+" | ";"+intTermString5+" | ";"+intTermString2+" | ";"+intTermString3+" | ";"+intTermString4+" | ";"+intTermString5+" | ";"+termString2+" | ";"+termString3+" | ";"+termString4+" | ";"+termString5+" | ";
"; buffer.append(termString0).append(sheet.getCell(0, i).getContents()).append(" | ");"+termString1+" | "+termString2+""+""+termString3+""+""+termString4+""+""+termString5+"
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd ");
dt.setDate(-1);
String yesterday = sdf.format(dt);
SendMailExcle.send("[email protected]", "lib", "RTYuio123", "请阅"+yesterday+"寿险电话中心日报报表", "D:/资料/Report_Day.xls", yesterday+"寿险电话中心日报报表.xls",buffer.toString());
// 返回值集合
}
public static void send(String cfrom, String username,
String password, String subject,
String attachmentPath, String attachmentName,String buffer) {
// 这里面使用新浪作为发送邮件的邮件服务器,其他的smtp服务器可以到相关网站上查到。
String host = "china.kjlink.com";
// 发送方邮箱地址(如[email protected].)
String from = cfrom;
// 收件人邮箱地址
//String to[] = serverMailBoxAddress;
// 发送者的邮箱用户名
String user = username;
// 发送者的邮箱密码
String ps = password;
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处显示过程信息,供调试使
// 用(有的时候网络连通性不够好,发送邮件可能会有延迟,在这里面会有所//提示,所以最好是加上这句,避免盲目的等待)
session.setDebug(true);
// 定义消息对象
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
message.setRecipients(RecipientType.TO,InternetAddress.parse("[email protected]"));
message.setRecipients(RecipientType.CC,InternetAddress.parse("[email protected]"));
message.setSubject(subject);
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
Multipart multipart = new MimeMultipart();
// 设置邮件的文本内容
BodyPart contentPart = new MimeBodyPart();
contentPart.setContent(buffer, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
// 添加附件
// 注意:下面定义的enc对象用来处理中文附件名,否则名称是中文的附//件在邮箱里面显示的会是乱码,
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setFileName("=?utf-8?B?"+ enc.encode(attachmentName.getBytes()) + "?=");
DataSource source = new FileDataSource(attachmentPath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(messageBodyPart);
// 将multipart对象放到message中
message.setContent(multipart);
// 发送邮件
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, user, ps);
transport.sendMessage(message, message.getAllRecipients());
//transport.send(message, InternetAddress.parse("[email protected],[email protected]"));
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
final String path="D:/资料/Report_Day.xls";
Timer timer = new Timer();
TimerTask tk=new TimerTask() {
@Override
public void run() {
try {
SendMailExcle.readExcel(path);
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
timer.schedule(tk, 1000);
}
}