简体   繁体   中英

ParseException when sending an email with zip file attachment?

I am getting an Exception when sending an email with a zipped file attachment, any suggestions?

Caused by: javax.mail.internet.ParseException: Expected '/', got null at javax.mail.internet.ContentType.(ContentType.java:102) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1322) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021) at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419) at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:13

private MimeBodyPart makeZipAttachment(AttachmentInfo attachmentInfo) throws IOException, MessagingException {
      ByteArrayOutputStream bos = null;
      ZipOutputStream zip = null;
      try
      {
         bos = new ByteArrayOutputStream();
         zip = new ZipOutputStream(bos);

         zip.putNextEntry(new ZipEntry(attachmentInfo.getName()));

         InputStream inputStream = attachmentInfo.getAttachment().getInputStream();
         byte[] buffer = new byte[1024];
         int len;
         while ((len = inputStream.read(buffer)) != -1) {
            zip.write(buffer, 0, len);
         }
         zip.closeEntry();
      }
      finally
      {
         if (bos != null)
            bos.close();
         if (zip != null)
            zip.close();
      }

      DataSource dataSource = new ByteArrayDataSource(bos.toByteArray(), "application/zip");
      MimeBodyPart mimeBodyPart = new MimeBodyPart();
      mimeBodyPart.setDataHandler(new DataHandler(dataSource));
      mimeBodyPart.setFileName(attachmentInfo.getName() + ".zip");
      mimeBodyPart.setHeader(CONTENT_TYPE, "application/zip");
      return mimeBodyPart;
   }

在我自己运行程序之前不能说太多,但是尝试设置内容以及像mimeBodyPart.setContent这样的内容

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM