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;
}
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
- javaxemail发送excel表格 工具类(不断更新中)
- 工资读取excel表格发送工具
- EXCEL宝典(不断更新中)
- 时间工具类(不断更新中)
- 图片工具类(不断更新中)
- Excel表格导入数据库工具类
- java Excel表格生成工具类
- springmvc excel表格导出工具类
- Excel表格处理工具
- JAVA工具类(5) --- 批量导入excel表格数据到数据库中
- Hibernate工具类的实现(不断更新中)
- IO流操作工具类(不断更新中)
- EXCEL中固定表格
- Excel表格中换行
- ExcelUtil工具类,使用Java代码操作Excel表格
- Apache POI封装工具类: 操作Excel表格
- Java实现对Excel表格的操作工具类
- EXCEL表格多表头工具类(JAVA)
- bugzilla使用SMTP发送邮件
- 简单的存储过程实例(插入100万数据时的状况)
- Android与服务器端数据交互(http协议整合struts2+android)
- 【返回顶部】 功能
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- javaxemail发送excel表格 工具类(不断更新中)
- 执行mount命令时找不到介质或者mount:no medium found的解决办法
- Python的zip函数
- 结构体之柔性数组
- 排序算法性能分析
- Unity3d教程Mecanim 动画体系赋予人物生命混合树 (Blend Tree)
- 【算法基础】基础查找算法
- 第五周作业——有向图邻接表表示及反向图构造
- printf——动态指定输出格式长度