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



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) {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) ctx.getExternalContext().getResponse();
OutputStream outputStream = null;
try {
    outputStream = response.getOutputStream();
} catch (IOException e) {
try {
    if (outputFormat.equals(OUTPUT_FORMAT_PDF)) {
        response.setHeader("Content-disposition","attachment; filename=" + reportName + ".pdf");
        JasperExportManager.exportReportToPdfStream(jPrint, outputStream);
} catch (JRException e) {
try {
} catch (IOException e) {

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.setDescription("Picture of John");
// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setAuthenticator(new DefaultAuthenticator("username", "password"));
email.addTo("", "John Doe");
email.setFrom("", "Me");
email.setSubject("The picture");
email.setMsg("Here is the picture you wanted");
// add the attachment
// send the email
zamansafari's picture
Joined: Feb 20 2013 - 8:01am
Last seen: 7 years 7 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 - 6 years 10 months ago

0 Answers:

No answers yet