邮件发送工具类
package com.chinasoft.hilink.util;
import com.chinasoft.hilink.bean.PushStrategyPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.*;
import java.util.Properties;
@Component
public class SendEmailUtil {
@Value("${mail.transport.protocol}")
private String protocol;
@Value("${mail.smtp.host}")
private String host;
@Value("${mail.smtp.port}")
private int port;
@Value("${mail.smtp.auth}")
private String auth;
@Value("${mail.smtp.ssl.enable}")
private String sslEnable;
@Value("${mail.debug}")
private String debug;
@Value("${mail.send.from}")
private String from;
@Value("${mail.smtp.server.user}")
private String user;
@Value("${mail.smtp.server.password}")
private String password;
private static final Logger logger = LoggerFactory.getLogger(SendEmailUtil.class);
public boolean send(PushStrategyPO pushStrategyPO, String path, String fileName) {
Properties properties = new Properties();
properties.put("mail.transport.protocol", protocol);
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth",auth );
properties.put("mail.smtp.ssl.enable",sslEnable);
properties.put("mail.debug", debug);
try {
Session session = Session.getInstance(properties);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
String recipient = pushStrategyPO.getRecipient();
InternetAddress[] internetAddresses1 = buildArray(recipient);
message.setRecipients(Message.RecipientType.TO, internetAddresses1
);
String cc = pushStrategyPO.getCC();
if (!StringUtils.isEmpty(cc)) {
InternetAddress[] internetAddresses = buildArray(cc);
message.setRecipients(Message.RecipientType.CC, internetAddresses);
}
message.setSubject(pushStrategyPO.getSubmit());
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(pushStrategyPO.getContent(), "text/html;charset=utf-8");
MimeBodyPart body1 = new MimeBodyPart();
body1.setDataHandler(new DataHandler(new FileDataSource(path)));
body1.setFileName(MimeUtility.encodeText(fileName));
MimeMultipart mm = new MimeMultipart();
mm.addBodyPart(mimeBodyPart);
mm.addBodyPart(body1);
message.setContent(mm);
Transport transport = session.getTransport();
transport.connect(user, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
logger.error("error is", e);
return false;
}
return true;
}
private InternetAddress[] buildArray(String emails) throws AddressException {
String[] emailArr = emails.trim().split(";");
InternetAddress[] internetAddresses = new InternetAddress[emailArr.length];
for (int i = 0; i < emailArr.length; i++) {
internetAddresses[i] = new InternetAddress(emailArr[i]);
}
return internetAddresses;
}
}
生成excel工具类
package com.chinasoft.hilink.util;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.List;
import java.util.Map;
public class exportUtils {
public static Workbook createWorkBook(List<Map<String, Object>> list,String []keys,String columnNames[],String fileName) {
HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet(fileName);
for(int i=0;i<keys.length;i++){
sheet.setColumnWidth(i, (int) (35.7 * 150));
}
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
Font f = wb.createFont();
Font f2 = wb.createFont();
f.setFontHeightInPoints((short)10);
f.setColor(IndexedColors.BLACK.getIndex());
f2.setFontHeightInPoints((short)10);
f2.setColor(IndexedColors.BLACK.getIndex());
cs.setFont(f);
cs.setVerticalAlignment(VerticalAlignment.CENTER);
cs2.setFont(f2);
Row row = sheet.createRow(0);
Cell cellHead = null;
for(int i=0;i<columnNames.length;i++){
cellHead = row.createCell(i);
cellHead.setCellValue(columnNames[i]);
cellHead.setCellStyle(cs);
}
for (int i = 0; i < list.size(); i++) {
Row row1 = sheet.createRow(i+1);
for(int j=0;j<keys.length;j++){
Cell cell = row1.createCell(j);
cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString());
cell.setCellStyle(cs2);
}
}
return wb;
}
public static Workbook createWorkBookByWorkbook(List<Map<String, Object>> list,String []keys,String columnNames[],String fileName,Workbook wb) {
Sheet sheet = wb.createSheet(fileName);
for(int i=0;i<keys.length;i++){
sheet.setColumnWidth(i, (int) (35.7 * 150));
}
CellStyle cs = wb.createCellStyle();
CellStyle cs2 = wb.createCellStyle();
Font f = wb.createFont();
Font f2 = wb.createFont();
f.setFontHeightInPoints((short)10);
f.setColor(IndexedColors.BLACK.getIndex());
f2.setFontHeightInPoints((short)10);
f2.setColor(IndexedColors.BLACK.getIndex());
cs.setFont(f);
cs.setVerticalAlignment(VerticalAlignment.CENTER);
cs2.setFont(f2);
Row row = sheet.createRow(0);
Cell cellHead = null;
for(int i=0;i<columnNames.length;i++){
cellHead = row.createCell(i);
cellHead.setCellValue(columnNames[i]);
cellHead.setCellStyle(cs);
}
for (int i = 0; i < list.size(); i++) {
Row row1 = sheet.createRow(i+1);
for(int j=0;j<keys.length;j++){
Cell cell = row1.createCell(j);
cell.setCellValue(list.get(i).get(keys[j]) == null?" ": list.get(i).get(keys[j]).toString());
cell.setCellStyle(cs2);
}
}
return wb;
}
}