Reputation: 319
Hi StackOverflow Community,
I'm trying to fix a problem that I have with Javax Mail, I have an SMTP Service that send mails with attachments. I send the PDF correctly and if I look at the email it shows that it's a PDF Document but when I press download save it as a File and cand be opened by acrobat reader properly.
Here's the code where I set the document
...
byte[] bytearray = "This is a PDF Document".getBytes();
ByteArrayDataSource bds = new ByteArrayDataSource(bytearray, "application/pdf");
attachmentBodyPart.setDataHandler(new DataHandler(bds));
attachmentBodyPart.setFileName("prueba");
attachmentBodyPart.setHeader("Content-Type", "application/pdf");
multipart.addBodyPart(attachmentBodyPart);
...
And here's the images of the mail received
WHat should I do? What I am missing?
Thanks in advise.
Upvotes: 0
Views: 581
Reputation: 4088
You are simply sending text as a pdf file, which as expected will not be a valid pdf that can be opened by any pdf reader.
You can use some library like iText to create a valid pdf file and then attach it to email.
Sample code using itext:
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("iTextHelloWorld.pdf"));
document.open();
Font font = FontFactory.getFont(FontFactory.COURIER, 16, BaseColor.BLACK);
Chunk chunk = new Chunk("Hello World", font);
document.add(chunk);
document.close();
For further reading : PDFs in java
Upvotes: 1