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

0

 

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();
}

send commons email function:

public void sendEmail {
// Create the attachment
EmailAttachment 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 message
MultiPartEmail 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 attachment
email.attach(attachment);
 
// send the email
email.send();
}
zamansafari's picture
Joined: Feb 20 2013 - 8:01am
Last seen: 6 years 9 months ago

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?

aprilevans587 - 5 years 11 months ago

0 Answers:

No answers yet
Feedback