Java Encoding Base64, MimeBodyPart Attachment

1.7k Views Asked by At

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?

0

There are 0 best solutions below