0 Replies Latest reply: Mar 7, 2013 6:05 AM by 995448 RSS

    file is damaged and could not be repaired, when emailed the PDF Document

    995448
      Hi i have the same problem , that what you have resolved, but it doesn't work for me...

      i.e, when i sent a mail from the below code , mail sent but when open it shows "**There was an error in opening this document. the file is damaged and could not be repaired."**

      can u please suggest to over come this problem.. and i m using IBM Websphere 8.

      please advice ....

      thanks alot in advance...

      here is the code :

      AstLogger logger = AstLogger.getInstance();
      logger.info(this.getClass(),"ReportBuilderUtils.sendMail Start....");
      boolean flag = false;
      try {
      javax.naming.InitialContext ctx = new javax.naming.InitialContext();
      javax.mail.Session session = (javax.mail.Session) ctx.lookup(AstProps.getInstance().getWebCollectLoginMailSession());

      Message msg = new MimeMessage(session);
      InternetAddress addressFrom = new InternetAddress(from);
      logger.info(this.getClass(), "Report Builder Utils - Email From :" + from);
      msg.setFrom(addressFrom);
      String log = "";
      double tempFilesize=0;
      for(String emailId : recipients) {
      InternetAddress addressTo = new InternetAddress(emailId);
      msg.addRecipient(Message.RecipientType.TO, addressTo);
      log += emailId + ",";
      }
      logger.info(this.getClass(), "Report Builder Utils - Email Recipients :" + log);
      // Setting the Subject and Content Type
      msg.setSubject(subject);
      logger.info(this.getClass(), "Report Builder Utils - Email Subject :" + subject);
      msg.setContent(message, "text/plain");
      msg.setHeader("Content-Type", "multipart/mixed");
      MimeBodyPart mbp1 = new MimeBodyPart();
      mbp1.setText(message);
      logger.info(this.getClass(), "Report Builder Utils - Email Message :" + message);
      mbp1.setDataHandler(new DataHandler(new HTMLDataSource(message)));
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp1);

      // constants to handle the large files
      if(tempFile.exists()){
      tempFilesize = tempFile.length();
      logger.info(this.getClass(), "Report Builder Utils - File Size :" +((tempFilesize/(1024*1024))) +"MB.");
      System.out.println("ATTACHMENT_SIZE ==>" +ATTACHMENT_SIZE);
      if(tempFilesize > ATTACHMENT_SIZE){
      flag = false;
      return flag;
      }
      }

      MimeBodyPart mbp_file = new MimeBodyPart();
      mbp_file.setHeader("Content-Type", "application/pdf");
      mbp_file.setHeader("Content-Transfer-Encoding", "Base64");
      mbp_file.setFileName(fileName);
      mbp_file.setDisposition(Part.ATTACHMENT);
      mbp_file.setDataHandler(new DataHandler(new FileDataSource(tempFile)));

      mp.addBodyPart(mbp_file);
      System.out.println("Mail Successfully sent to :"+recipients);
      msg.saveChanges();
      msg.setContent(mp);
      // even tried by placing here
      // mbp_file.setHeader("Content-Transfer-Encoding", "base64"); base64 is not case-sensitive i hope , i read out on some books
      Transport.send(msg);
      logger.info(this.getClass(), "Report Builder Utils - Email Send successfully...");
      flag = true;
      }catch (Exception e) {
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      e.printStackTrace(pw);
      logger.error(this.getClass(), sw.toString());
      }
      AstLogger.getInstance().info(this.getClass(),"ReportBuilderUtils.sendMailAsAttachment End.");
      return flag;
      }

      Edited by: 992445 on Mar 7, 2013 4:05 AM