Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add attachment to mail from bytearrayoutputstream

I am trying to send an email with attachment like this :

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, false, CharEncoding.UTF_8);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
message.addAttachment("facture.pdf",  new ByteArrayResource(IOUtils.toByteArray(is)));

I am getting an error :

java.lang.IllegalStateException: Not in multipart mode - create an appropriate MimeMessageHelper via a constructor that takes a 'multipart' flag if you need to set alternative texts or add inline elements or attachments.

Is there a way to make it work keeping the addAttachment method?

like image 833
user1260928 Avatar asked Oct 28 '25 16:10

user1260928


1 Answers

It seems from the documentation of MimeMessageHelper, that you only have to pass a true flag.

MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, CharEncoding.UTF_8);
like image 189
Arnaud Avatar answered Oct 31 '25 06:10

Arnaud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!