+-
java – 以Pdf附件的形式发送电子邮件作为流
我想发送一个Pdf作为电子邮件附件(我正在使用 JavaMail API).我把Pdf(由jasper生成)作为byte [].

public InputStream exportPdfToInputStream(User user) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, JRException, IOException{
        JasperPrint jasperPrint = createJasperPrintObject(user);
        byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrint);
        return new ByteArrayInputStream(pdfByteArray);
    }

这是我用来构建将成为附件的MimeBodyPart的代码:

    if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
        MimeBodyPart attachment = new MimeBodyPart(arrayInputStream);
        attachment.setHeader("Content-Type", "application/pdf");
        mimeMultipart.addBodyPart(attachment);
    }

这段代码给了我这个错误:

javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.IOException: Error in encoded stream: needed at least 2 valid base64 characters, but only got 1 before padding character (=), the 10 most recent characters were: "\24\163\193\n\185\194\216#\208="
最佳答案
您使用的构造函数用于从传输中解析mime部分.

你的第二个例子应该正确.你可以考虑一下

>不要转换为InputStream并返回,这将产生不必要的副本
>添加处置(例如bp.setDisposition(Part.ATTACHMENT);)

点击查看更多相关文章

转载注明原文:java – 以Pdf附件的形式发送电子邮件作为流 - 乐贴网