spring,jasperreports发送报表邮件

前提:
1、版本要一致,偶这用的是jasperreports-4.1.2,iReport-4.1.1
2、编译好***.jasper
3、/html/image.jsp?image= 替换,是为了显示报表图片,之前用此jsp作为读取报表图片用。


public void process() {
Connection conn = null;

try {
//报表内嵌了SQL,只要传入参数,数据库连接即可。
conn = dataSource.getConnection();

Map parameters = new HashMap();
parameters.put("startTime", "20110101");
parameters.put("endTime", "20111001");

String jasperFile = "d://*****/" + "*****.jasper";

File reportFile = new File(jasperFile);
if (!reportFile.exists())
throw new JRRuntimeException("File jasper not found. The report design must be compiled first.");

JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperFile,
parameters,
conn
);

JRHtmlExporter exporter = new JRHtmlExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
ByteArrayOutputStream htmlOutputData = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, htmlOutputData);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, imagePath);
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, true);
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");

Map imageNameMap = new HashMap();
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imageNameMap);
exporter.exportReport();
//attach the html data from htmlOutputData

byte[] imageData = null;
for (Iterator it = imageNameMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry) it.next();
String imageName = (String) entry.getKey();
imageData = (byte[]) entry.getValue();
//attach imageData using imageName as Content-ID
}


sendEmail(htmlOutputData.toByteArray(), imageData, imageNameMap);

} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch (Exception e) {
}
}
}



public void sendEmail(final byte[] attachmentData, final byte[] imageData, final Map<String, byte[]> imageNameMap) throws MessagingException {

String host = "smtp.126.com";
final String account = "****@126.com";
final String toAddress = "****@163.com";
String username = ""****@126.com";
String password = "******";


JavaMailSenderImpl sender = new JavaMailSenderImpl();

sender.setHost(host);
sender.setUsername(username);
sender.setPassword(password);

MimeMessagePreparator preparator = new MimeMessagePreparator() {

public void prepare(MimeMessage mimeMessage) throws Exception {
Multipart multipart = new MimeMultipart();

MimeBodyPart htmlAttachment = new MimeBodyPart();
String html = new String(attachmentData, "utf-8");
for (String imageName : imageNameMap.keySet()) {
html = html.replace("/html/image.jsp?image=" + imageName, "cid:" + imageName);
}
htmlAttachment.setContent(html, "text/html;charset=\"utf-8\""); //
multipart.addBodyPart(htmlAttachment);
//当你有多个报表图片时
for (String imageName : imageNameMap.keySet()) {
MimeBodyPart imagebody = new MimeBodyPart();
imagebody.setContent(imageData, "application/octet-stream");
imagebody.setHeader("Content-ID", "<" + imageName + ">");

multipart.addBodyPart(imagebody);
}


mimeMessage.setContent(multipart);

mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddress));
mimeMessage.setFrom(new InternetAddress(account));

mimeMessage.setSubject("test send jasperreports mail..");
}
};


try {
sender.send(preparator);
} catch (MailException ex) {
ex.printStackTrace();
}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值