开发者社区> 问答> 正文

Java编码Base64,MimeBodyPart附件

我有一个用Base64编码的PDF文件,现在我想发送PDF并允许从邮件打开。

我正在阅读此问题,但不适用于我```js https://stackoverflow.com/a/9124625/811293

Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setSentDate(new Date()); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(sendTo)); message.setSubject("Subject - " + new Date()); Multipart multipart = new MimeMultipart();

MimeBodyPart mimeBodyPart = new MimeBodyPart(); mimeBodyPart.setContent("contentMessage", "text/html"); // YES FOR NOW SIMPLE MESSAGE multipart.addBodyPart(mimeBodyPart);

MimeBodyPart attachmentOnFly = new MimeBodyPart();

/* //In the future the PDF will be created on Fly (Will not be stored) InputStream inputStream = new FileInputStream("/path/to/encoded/Base64/file.pdf");//new ByteArrayInputStream("Bytes from source".getBytes()); ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(inputStream, "application/pdf"); attachmentOnFly.setDataHandler(new DataHandler(byteArrayDataSource)); */

attachmentOnFly.attachFile(new File("/path/to/encoded/Base64/file.pdf")); attachmentOnFly.setHeader("Content-Type", "application/pdf"); attachmentOnFly.setHeader("Content-Transfer-Encoding", "base64"); attachmentOnFly.setDisposition(Part.ATTACHMENT); attachmentOnFly.setFileName("myFileName.pdf"); multipart.addBodyPart(attachmentOnFly);

message.setContent(multipart); Transport.send(message);

我的问题是,当收到邮件时,我无法打开PDF,但是使用Telnet替代方法却可行。

如何发送Enconded Base64 PDF,并且可以从邮件中打开它?

展开
收起
垚tutu 2019-12-04 16:57:57 1201 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载