Reputation: 512
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)
Upvotes: 1
Views: 1507
Reputation: 512
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")
Upvotes: 1