javaxemail发送excel表格 工具类(不断更新中)

来源:互联网 发布:神尔天才听读机淘宝价 编辑:程序博客网 时间:2024/06/06 03:51
package com.util.email;


import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


import com.util.excel.POIExcelMakerUtil;
/**
 * @author swj
 */
public class EmailUtil {
String username;
String password;
String fromEmail;
String smtpServer;
int smtpport;
String sourcePath;
int emailcolumn;
String sheetName;
boolean debug;
Properties props = new Properties();
Session session;
public EmailUtil(String username, String password) {
this.username = username;
this.password = password;
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
this.session = Session.getDefaultInstance(props);
}

/**

* @param username 用户名
* @param password 密码
* @param fromemail 发件人邮箱地址
* @param smtpserver SMTP服务器
* @param smtpport SMTP服务端口
* @param sourcepath 资源路径
* @param emailcolumn email列数
* @param sheetname sheet名字
* @param debug是否开启debug
*/
public EmailUtil(String username, String password, String fromemail,
String smtpserver, String smtpport, String sourcepath,
String emailcolumn, String sheetname,String debug) {
this(username, password);
this.fromEmail = fromemail;
this.smtpServer = smtpserver;
this.smtpport = Integer.parseInt(smtpport);
this.sourcePath = sourcepath;
this.emailcolumn = Integer.parseInt(emailcolumn);
this.sheetName = sheetname;
this.debug = "true".equals(debug)?true:false;
}




public  void sendEmail() throws Exception{
Transport transport = createTransport();
transport.connect(smtpServer, smtpport, username, password);
processAndSend(transport);
transport.close();
}

private Transport createTransport() throws NoSuchProviderException{
Session session = Session.getDefaultInstance(props);
session.setDebug(this.debug);
Transport transport = session.getTransport();
return transport;
}
private void processAndSend(Transport transport) throws Exception{
List<List<String>> excel = process();
send(transport,excel);
}
private List<List<String>> process() throws Exception{
List<List<String>> excel = new ArrayList<List<String>>();
POIExcelMakerUtil poiExcel = new POIExcelMakerUtil(sourcePath);
int maxRowNum = poiExcel.getRowNum(sheetName);
for(int i=0;i<maxRowNum;i++){
List<String> data = poiExcel.getRowCellValues(sheetName, i);
if(data!=null){
excel.add(data);
}
}
return excel;
}
private void send(Transport transport, List<List<String>> excel) throws MessagingException{
List<String> title = excel.get(0);
for(int i=1;i<excel.size();i++){
List<String> data = excel.get(i);
String html_msg = createHTML(title,data);
Message message = createMessage(session,html_msg);
transport.sendMessage(message, new Address[] { new InternetAddress(data.get(emailcolumn)) });
}
}
private Message createMessage(Session session, String html_msg) throws MessagingException {
Message msg = new MimeMessage(session);
msg.setContent(html_msg, "text/html;charset=gbk");
msg.setFrom(new InternetAddress(fromEmail));
return msg;
}




private String createHTML(List<String> title, List<String> data) {
int maxRowNum = data.size();
String html_msg="";
for(int i=1;i<maxRowNum;i++){
html_msg = "<table border=\"1\" width='80%' height='80'>";
html_msg = html_msg+"<tr bgcolor='#B6DDE6'>";
for(int column=0;column<title.size();column++){
html_msg = html_msg +"<td width='12%'>"+title.get(column)+"</td>";
}
html_msg = html_msg+"</tr>";
html_msg = html_msg+"<tr>";
for(int column=0;column<data.size();column++){
html_msg = html_msg +"<td>"+data.get(column)+"</td>";
}
html_msg = html_msg+"</tr>";
html_msg = html_msg + "</table>";
System.err.println(html_msg);
}
return html_msg;
}


}


还有很多问题有待于完善

0 0
原创粉丝点击