Desidero inviare un Pdf come allegato di posta elettronica (sto utilizzando l'API JavaMail). Ho il Pdf (generato da Jasper) come byte[]
.Invia e-mail in allegato Pdf come stream
public InputStream exportPdfToInputStream(User user) throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, JRException, IOException{
JasperPrint jasperPrint = createJasperPrintObject(user);
byte[] pdfByteArray = JasperExportManager.exportReportToPdf(jasperPrint);
return new ByteArrayInputStream(pdfByteArray);
}
Ecco il codice che sto usando per costruire la MimeBodyPart
che sarà l'allegato:
if (arrayInputStream != null && arrayInputStream instanceof ByteArrayInputStream) {
MimeBodyPart attachment = new MimeBodyPart(arrayInputStream);
attachment.setHeader("Content-Type", "application/pdf");
mimeMultipart.addBodyPart(attachment);
}
Questo codice mi dà questo errore:
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="
@mtraut: hai detto di non convertirlo in InputStream e tornare indietro, ma come posso farlo senza un InputStream? – Atticus
utilizza il costruttore ** ByteArrayDataSource (byte [] data, tipo String) ** e il "pdfByteArray" – mtraut
@mtraut: Grazie, è ciò che ho già inviato nella mia risposta. Ma grazie comunque. Anche se c'è un altro modo ancora più semplice per farlo! – Atticus