1

Im trying to send raw emails with images as attachments stored in s3 using I am trying to send raw emails with images as attachments stored in s3 using java. However, when receiving email images are corrupted. I tried using file stored in my disk and it worked correctly but it's not working for files stored in S3. Here's the code I'm using.

                    MimeBodyPart att = new MimeBodyPart();
                    att.setFileName(attachment.getFileName());
                    String attContent = s3.getObjectAsString(emailServiceConfiguration.getAttachmentS3Bucket(), attachment.getPath());
                    DataSource source = new ByteArrayDataSource(attContent.getBytes(),"application/png");
                    att.setDataHandler(new DataHandler(source));
                    msg.addBodyPart(att);

Please help, I am stuck here for quite some time.

24Circles
  • 11
  • 3

2 Answers2

0

Q: Can Amazon SES send emails with attachments?

You can also send email with attachments programmatically. To include an attachment in your email, construct a new multipart email message. In the message, include a MIME part that contains an appropriate Content-Type header, along with the MIME-encoded content. Next, use the Content-Disposition header to specify whether the content is to be displayed inline or treated as an attachment.

Once you've composed your message, you can use the SendRawEmail API operationto send

Sending raw email using the Amazon SES API

This post covers how to forward emails with attachment in S3 Forward Incoming Email to an External Destination

Relevant post with necessary code How to convert raw emails (MIME) from AWS SES to Gmail?

samtoddler
  • 8,463
  • 2
  • 26
  • 21
0

Instead of getting the bytes from string, use byte array

MimeBodyPart att = new MimeBodyPart();
att.setFileName(attachment.getFileName());
S3Object s3Object = s3.getObject(emailServiceConfiguration.getAttachmentS3Bucket(), attachment.getPath());
byte[] byteArray = IOUtils.toByteArray(s3Object.getObjectContent());
DataSource source = new ByteArrayDataSource(byteArray,"application/png");
att.setDataHandler(new DataHandler(source));
msg.addBodyPart(att);
neil
  • 123
  • 9