POI不发送xls文件作为附件,以字符串形式发送

时间:2013-07-18 12:01:53

标签: apache-poi javamail

我的一个初级代码没有发送excel文件作为附件。它正在发送像

这样的文件

------ = _ Part_0_2066339629.1374147892060 内容类型:text / plain; name =“Service_Change_Alert_Thu 7月18日17:14:50 IST 2013.xlsx” 内容传输编码:base64 内容 - 处理:附件; filename =“Service_Change_Alert_Thu 7月18日17:14:50 IST 2013.xlsx”

UEsDBBQACAAIANqJ8kIAAAAAAAAAAAAAAAARAAAAZG9jUHJvcHMvY29yZS54bWytkV1LwzAUhu / 7 K0Lu2yTr1BHaDlEGguLADsW7kB7bYvNBEu3892bdrCheennyPu / D4aRY79WA3sH53ugSs4xiBFqa ptdtiXf1Jl3hdZUkhTQOts5YcKEHj2JL + xJ3IVhOiJcdKOGzGOuYvBinRIija4kV8lW0QBaUnhMF QTQiCHKwpXbW4aOPS / vvykbOSvvmhknQSAIDKNDBE5Yx8s0GcMr / WZiSmdz7fqbGcczGfOLiRow8 3D0 + TMunvfZBaAm4ShAqTnYuHYgADYoOHj4slPgrecyvrusNrhaU5Sm9SNmqZowvl / yMPhfkV //ķ

功能正在关注

          public void sendSeviceabilityMail(List<OctpinSaveBean> datalist)
                 throws MessagingException {
          Session session = null;
          Map<String, String> utilsMap = ApplicationBean.utilsProperties;
          if (utilsMap == null || utilsMap.size() == 0)
                 utilsMap = ApplicationBean.getUtilsPropertyFileValues();
          smtpHost = utilsMap.get("SMTPHost");
          to = utilsMap.get("To");
          try {
                 if (smtpHost != null && to != null) {

                       Date date = new Date();
                       XSSFWorkbook updateDataBook = 
                       updatedServiceabilityExcel(datalist);
                       Properties props = System.getProperties();
                       props.put("mail.smtp.host", smtpHost);
                       props.put("To", to);
                       session = Session.getInstance(props, null);
                       String str = "strstr";
                       Multipart multipart = new MimeMultipart();
                       BodyPart messageBodyPart1 = new MimeBodyPart();
                       messageBodyPart1.setContent(str, "text/html");
                       BodyPart messageBodyPart = new MimeBodyPart();
                       ByteArrayOutputStream baos = new ByteArrayOutputStream();
                       updateDataBook.write(baos);
                       byte[] bytes = baos.toByteArray();
                       DataSource ds = new ByteArrayDataSource(bytes,  
                        "application/vnd.ms-excel");
                       DataHandler dh = new DataHandler(ds);
                       messageBodyPart.setDataHandler(dh);
                       String fileName = "Service_Change_Alert_" + date+".xlsx";
                       messageBodyPart.setFileName(fileName);
                       messageBodyPart.setHeader("Content-disposition", "attachment; 
                        filename=\"" + fileName + "\"");
                       multipart.addBodyPart(messageBodyPart1);
                       multipart.addBodyPart(messageBodyPart);
                       if (to != null && to.length() > 0) {
                              MimeMessage msg = new MimeMessage(session);
                              msg.setFrom(new InternetAddress("tech_noida 
                       <tech_noida@xyz.com>"));
                              String[] toArray = to.split(",");
                              InternetAddress[] address = new 
                          InternetAddress[toArray.length];
                              for (int i = 0; i < toArray.length; i++) {
                                     address[i] = new InternetAddress(toArray[i]);
                              }
                              msg.setRecipients(Message.RecipientType.TO, address);
                              msg.setSubject("Updated Serviceability Alert!!!");
                              msg.setContent(multipart);
                              msg.setSentDate(date);
                              try {
                                     Transport.send(msg);
                                     logger.info("Mail has been sent about the        updated               serviceability alert to :" + to);
                              } catch (Exception e1) {
                                     e1.printStackTrace();
                              }
                       }
                 }
          } catch (Exception e) {
                 e.printStackTrace();
          }

   }

1 个答案:

答案 0 :(得分:1)

问题与POI无关,但与JavaMail有关(加上不同的电子邮件客户端处理规范和格式错误的电子邮件的方式)。试试这个:

Multipart multipart = new MimeMultipart();

MimeBodyPart html = new MimeBodyPart();
// Use actual html not "strstr"
html.setContent("<html><body><h1>Hi</h1></body></html>", "text/html");
multipart.addBodyPart(html);

// ...

// Joop Eggen suggestion to avoid spaces in the file name
String fileName = "Service_Change_Alert_" 
     + new SimpleDateFormat("yyyy-MM-dd_HH:mm").format(date) + ".xlsx"; 

ByteArrayOutputStream baos = new ByteArrayOutputStream();
updateDataBook.write(baos);
byte[] poiBytes = baos.toByteArray();  
// Can be followed by the DataSource / DataHandler stuff if you really need it

MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName(filename);
attachment.setContent(poiBytes, "application/vnd.ms-excel");
//attachment.setDataHandler(dh);
attachment.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(attachment);

更新:

另外,请不要忘记在发送邮件之前调用saveChanges来更新标题。

msg.saveChanges(); 

有关详细信息,请参阅this answer