Send e-mail in Pdf attachment as stream

后端 未结 2 1152
日久生厌
日久生厌 2020-12-25 08:24

I want to send a Pdf as an e-mail attachment (I am using the JavaMail API ). I have the Pdf (generated by jasper) as an byte[].

public InputStre         


        
相关标签:
2条回答
  • 2020-12-25 08:40

    The constructor you used is for parsing a mime part from the transport.

    Your second example should work out right. You may consider

    • not to convert to InputStream and back, this will make unnecessary copies
    • add a disposition ( e.g. bp.setDisposition(Part.ATTACHMENT); )
    0 讨论(0)
  • 2020-12-25 09:04

    I have found a solution as suggested in this thread. It seems that there is a DataSource class created just for this purpose. Hope this example will help others also.

        if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
            // create the second message part with the attachment from a OutputStrean
            MimeBodyPart attachment= new MimeBodyPart();
            ByteArrayDataSource ds = new ByteArrayDataSource(arrayInputStream, "application/pdf"); 
            attachment.setDataHandler(new DataHandler(ds));
            attachment.setFileName("Report.pdf");
            mimeMultipart.addBodyPart(attachment);
        }
    
    0 讨论(0)
提交回复
热议问题