0

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

------=_Part_0_2066339629.1374147892060 内容类型:文本/纯文本;name="Service_Change_Alert_Thu Jul 18 17:14:50 IST 2013.xlsx" 内容传输编码:base64 内容处置:附件;文件名="Service_Change_Alert_Thu Jul 18 17:14:50 IST 2013.xlsx"

UEsDBBQACAAIANqJ8kIAAAAAAAAAAAAAAAARAAAAZG9jUHJvcHMvY29yZS54bWytkV1LwzAUhu/7 K0Lu2yTr1BHaDlEGguLADsW7kB7bYvNBEu3892bdrCheennyPu/D4aRY79WA3sH53ugSs4xiBFqa ptdtiXf1Jl3hdZUkhTQOts5YcKEHj2JL+xJ3IVhOiJcdKOGzGOuYvBinRIija4kV8lW0QBaUnhMF QTQiCHKwpXbW4aOPS/vvykbOSvvmhknQSAIDKNDBE5Yx8s0GcMr/WZiSmdz7fqbGcczGfOLiRow8 3d0+TMunvfZBaAm4ShAqTnYuHYgADYoOHj4slPgrecyvrusNrhaU5Sm9SNmqZowvl/yMPhfkV//k

功能如下

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

   }
4

1 回答 1

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

有关更多详细信息,请参阅此答案

于 2013-07-18T13:06:06.113 回答