0

Am trying to send S3 pdf documents as e-mail attachment. I use the signed URL of the document and build MimeBodyPart as below.

 Multipart multipart = new MimeMultipart();

 // have another MimeBodyPart for text/html content



BodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(new URLDataSource(attachmentUrl)));
attachment.setFileName(<filename>);
attachment.setDisposition(MimeBodyPart.ATTACHMENT);
attachment.setContent(<filename>,"application/pdf");
multipart.addBodyPart(attachment)

The problem am facing is the attachment is successfully attached but without any content. I meant it's always 1KB. And unable to open/view anything.

The above problem is faced for pdf files alone. But if I give some image URL and remove the setContent, setDisposition everything is working as expected.

Debugged some more looking at raw email could see the headers for the attachment are different.

When attached the image from this URL

Content-Type: image/png; name=googlelogo_color_272x92dp.png
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=googlelogo_color_272x92dp.png

But when I attach any PDf/S3 signed one via a URL I get as below

Content-Type: application/pdf; name=structures-and-c.pdf
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=structures-and-c.pdf

The exception when I get when I don't explicitly set the content type. The same is not being complained for an image.

javax.mail.internet.ParseException: In Content-Type string <pdf>, expected '/', got null

    at javax.mail.internet.ContentType.<init>(ContentType.java:80)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1486)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1148)
    at javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:498)
    at javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1509)
    at javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2238)
    at javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2198)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1877)
    at javax.mail.internet.MimeMessage.writeTo(MimeMessage.java:1854)
balaaagi
  • 502
  • 11
  • 21
  • Remove these 2 lines: `attachment.setDisposition(MimeBodyPart.ATTACHMENT);` and `attachment.setContent(,"application/pdf");`. Replace them with this one line: `attachment.setFileName();`. – andrewJames Apr 08 '21 at 21:18
  • That did not help. If we remove those for PDF content-type is going as null and it fails.I tried by including the `setFileName` along with the above setter result remains same. – balaaagi Apr 09 '21 at 02:35
  • OK - understood. When I used the code in the question, I ggot the same issue as you. When I make my changes the issue is resolved for me. (I am assuming the signed URL is valid, by the way.) – andrewJames Apr 09 '21 at 12:19
  • I tried with a signed URL as well as any pdf in a public URL. Facing a similar issue. How does your signed URL look like? Can you share? – balaaagi Apr 09 '21 at 12:44
  • Sounds like the signed URL issue is not relevant if your code fails with unsigned URLs to PDFs, elsewhere. (I don't have an unexpired signed URL to share). – andrewJames Apr 09 '21 at 12:56
  • 1
    Figured out. It was more of the signed URL issue where the content type was not set properly. – balaaagi Apr 09 '21 at 13:39

1 Answers1

1

If you are using S3 signed url for PDF using the above way no need to set the below.

attachment.setDisposition(MimeBodyPart.ATTACHMENT);
attachment.setContent(<filename>,"application/pdf");

Ensure when you are generating signed URL it sets the below properly

GetObjectRequest.builder()
                    .bucket(bucketName)
                    .key(objectName)
                    .responseContentDisposition("attachment;filename=" + objectName)
                    .responseContentEncoding("base64")
                    .responseContentType("application/pdf")
balaaagi
  • 502
  • 11
  • 21