Note that there are some explanatory texts on larger screens.

plurals
  1. POJava mail PDF attachment not working
    text
    copied!<p>I am generating a PDF and trying to attach it to a mail as well as download it from browser using java. Download from browser works fine, but attaching to mail is where I am facing an issue. The file is attached. Attachment name and size of the file are intact. The problem is when I open the PDF from mail attachment, it shows nothing. correct number of pages with no content. When I attach the file downloaded from browser by hardcoding, it works fine. So I suppose the problem is not with the PDF generation. I tried opening both(one downloaded from browser and the other downloaded from mail) the files using comparing tool beyond compare. The one downloaded from mail shows conversion error. When I open with notepad++, both show different encoding. I not very familiar with these encoding thing. I suppose it is something to do with encoding.</p> <p>I also observed that the content in mail download is same as the one at PDF generation. But the one at browser download is different.</p> <p>An excerpt of what I get on browser download is as below(The content is too large to paste) </p> <pre><code>%PDF-1.4 %âãÏÓ 4 0 obj &lt;&lt;/Type/XObject/ColorSpace/DeviceRGB/Subtype/Image/BitsPerComponent 8/Width 193/Length 11222/Height 58/Filter/DCTDecode&gt;&gt;stream ÿØÿà </code></pre> <p>An excerpt of what I get on mail download is as below </p> <pre><code>%PDF-1.4 %???? 4 0 obj &lt;&lt;/Type/XObject/ColorSpace/DeviceRGB/Subtype/Image/BitsPerComponent 8/Width 193/Length 11222/Height 58/Filter/DCTDecode&gt;&gt;stream ???? </code></pre> <p>I am using Spring MimeMessageHelper to send the message. I am using the below method to add attachment</p> <pre><code>MimeMessageHelper.addAttachment(fileName, new ByteArrayResource(attachmentContent.getBytes()), "application/pdf"); </code></pre> <p>I've also tried another way of attaching but in vain</p> <pre><code>DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf"); MimeBodyPart pdfBodyPart = new MimeBodyPart(); pdfBodyPart.addHeader("Content-Type", "application/pdf;charset=UTF-8"); pdfBodyPart.addHeader("Content-disposition", "attachment; filename="+fileName); pdfBodyPart.setDataHandler(new DataHandler(dataSource)); pdfBodyPart.setFileName(fileName); mimeMessageHelper.getMimeMultipart().addBodyPart(pdfBodyPart); </code></pre> <p>Any help would be greatly appreciated. Thanks in advance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload