Java Encoding Base64, MimeBodyPart Attachment

纵饮孤独 提交于 2019-12-12 01:12:49

问题


I have a PDF Files Encoded with Base64, now I want to send the PDF's and allow to open from mail.

I was reading this question, But IS not working for me 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);

My problem, is when the mail isreceived, I can't open the PDF, But using Telnet alternative is working.

How Send Enconded Base64 PDF and that can be opened from the mail?

来源:https://stackoverflow.com/questions/59166613/java-encoding-base64-mimebodypart-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!