Send e-mail in Pdf attachment as stream

后端 未结 2 1165
日久生厌
日久生厌 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 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);
        }
    

提交回复
热议问题