0

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

我正在阅读这个问题,但不适用于我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 并且可以从邮件中打开?

4

0 回答 0