InputStream inputStream = new FileInputStream(fileName);
String inputStreamToString = inputStream.toString();
inputStream.close();
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(inputStreamToString.getBytes());
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.setDisposition(MimeBodyPart.INLINE);
attachment.setFileName(fileName);
multipart.addBodyPart(attachment);
}
mimeMsg.setContent(multipart);
我将 PDF 作为字节获取并使用 ByteArrayDataSource。
我还将处置设置为内联。