Jump to content
Changes to the Jaspersoft community edition download ×

Send Email with pdf and excel attachments using Apache Commons Email and Jasper Report


zamansafari

Recommended Posts

 

In my application, I am able to send emails and prepare Jasper Reports. I can send simple text based emails and even attach an image file to the content using Apache Commons-Emails. Also I am able to prepare reports using Jasper Reports with pdf and excel formats. The reported file is downloaded to my pc in any format without any problem. However, my question is I want to send the prepared report via the Apache Commons-Email to a user. That is, the report is ready and I can export its pdf format. I just want the exported pdf path and put it to my mail function. I am a new starter and I couldn't bind the two ready pieces in my app. Could you please shed a light to the matter. Any help would greatly be appreciated. Thank you in very much...

Below I am sending the prepared report and email functions so that it may help. In the "sendEmail()" function I just want to put prepared report path instead of image path in the example below. Or any other ways that could help. 

Prepare Jasper Report function:

public void generateReport(String reportPath, @SuppressWarnings("rawtypes") List beanList, String outputFormat, String reportName){try {    JasperReport jReport = JasperCompileManager.compileReport(reportPath);    JasperPrint jPrint = JasperFillManager.fillReport(jReport, jasperParameter, new JRBeanCollectionDataSource(beanList));} catch (JRException e) {    e.printStackTrace();}FacesContext ctx = FacesContext.getCurrentInstance();HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();OutputStream outputStream = null;try {    outputStream = response.getOutputStream();} catch (IOException e) {    e.printStackTrace();}try {    if (outputFormat.equals(OUTPUT_FORMAT_PDF)) {        response.setContentType("application/pdf");        response.setHeader("Content-disposition","attachment; filename=" + reportName + ".pdf");        JasperExportManager.exportReportToPdfStream(jPrint, outputStream);    } } catch (JRException e) {    e.printStackTrace();}try {    outputStream.flush();    outputStream.close();    FacesContext.getCurrentInstance().responseComplete();} catch (IOException e) {    e.printStackTrace();}[/code]

send commons email function:

public void sendEmail {// Create the attachmentEmailAttachment attachment = new EmailAttachment();attachment.setPath("mypictures/john.jpg"); // here I want to set the prepared report path so that it could be sent to the user.attachment.setDisposition(EmailAttachment.ATTACHMENT);attachment.setDescription("Picture of John");attachment.setName("John");// Create the email messageMultiPartEmail email = new MultiPartEmail();email.setHostName("smtp.googlemail.com");email.setSmtpPort(465);email.setAuthenticator(new DefaultAuthenticator("username", "password"));email.setSSLOnConnect(true);email.addTo("jdoe@somewhere.org", "John Doe");email.setFrom("me@apache.org", "Me");email.setSubject("The picture");email.setMsg("Here is the picture you wanted");// add the attachmentemail.attach(attachment);// send the emailemail.send();}[/code]
Link to comment
Share on other sites

  • 9 months later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hey, I did not know the answer of your question. But I am on a similar project. I want to know that when we E-mail others with PDF attachment. In general, people can directly read the PDF file by clicking attached PDF file, right. But my client said they cannot open and read PDFon E-mail. But they can open it after they download it to computer. Have you met similar issue?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...