JasperReport: Fill, Create PDF,-attach to email, send
Tháng tám 16, 2005
JasperPrint jasperPrint = JasperFillManager.fillReport(pathToJapserReportFilename, parameters, jrBeanArrayDataSource);
byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);
mailer.sendMail(MAIL_FROM, to_email_address, subject, MAIL_BODY, pdfBytes, filename + “.pdf”, “application/pdf”);
The actual emailing part is a little more complex. You’ll need a javax.activation.DataSource implementation that handles byte[]’s.
Here’s one I wrote, or you can use org.apache.commons.mail.ByteArrayDataSource.
public static DataSource getDataSource(final byte[] bytes, final String theContentType) {
return new DataSource() {
public InputStream getInputStream()
{
return new ByteArrayInputStream(bytes);
}
public OutputStream getOutputStream()
{
throw new UnsupportedOperationException();
}
public String getContentType() {
return theContentType;
}
public String getName() {
throw new UnsupportedOperationException();
}
};
}
And then here’s the code from my MailerBean (this is running inside JBoss).
Once you have the datasource working, this is all pretty standard Java mailing stuff.
public void sendMail(String from,String to,String subject,String bodyText,
byte[] theAttachmentBytes, String theFilename, String theContentType)
{
javax.mail.Session session = null;
try {
session = (Session)PortableRemoteObject.narrow(new InitialContext().lookup(“java:Mail”), Session.class);
} catch (javax.naming.NamingException e) {
e.printStackTrace();
}
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = {new InternetAddress(to)};
message.setRecipients(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setSentDate(new Date());
// create and fill the first message part
MimeBodyPart body = new MimeBodyPart();
body.setText(bodyText);
body.setDisposition(Part.INLINE);
body.setHeader(“Content-Type”, “text/plain”);
// turn the File into a Part
MimeBodyPart part = new MimeBodyPart();
part.setFileName(theFilename);
part.setDisposition(Part.ATTACHMENT);
DataSource dataSource = getDataSource(theAttachmentBytes, theContentType);
part.setDataHandler(new DataHandler(dataSource));
// set the header for the content type, using the type
// determined by the file name to mime type mapper that
// is inherent in the FileDataSource.
//part.setHeader(“Content-Type”, dataSource.getContentType());
part.setHeader(“Content-Type”, dataSource.getContentType());
// set the transfer encoding so that binary data will
// come through cleanly. in cases where the content
// type is a “text” type you may not need to do base64
// encoding, but it’s simple and safe to always encode
// attachments.
part.setHeader(“Content-Transfer-Encoding”, “base64″);
Multipart multipart = new MimeMultipart();
message.setContent(multipart);
message.setHeader(“Content-Type”, multipart.getContentType());
Multipart mp = new MimeMultipart();
mp.addBodyPart(body);
mp.addBodyPart(part);
// add the Multipart to the message
message.setContent(mp);
Transport.send(message);
} catch (javax.mail.MessagingException e) {
Debug.error(“MailerBean.sendHtmlMail”, e);
}
Entry Filed under: Jasper Report. Thẻ: Jasper Report.
Trackback this post | Subscribe to the comments via RSS Feed