Search code examples
javaspringemailjakarta-mail

JavaMail - MimeMessageHelper Attachment does not get sent with mail


I can send emails just fine but the attachment is never added to the mail. Below is my code, created a tempFile for testing purpose.

What did i miss?

I tried other ways to add attachments as well creating a seperate BodyPart, using FileInputStreamResource,...

 MimeMessage msg = sender.createMimeMessage();
 MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");

 helper.setFrom(eMail);
 helper.setTo(eMail);
 helper.setSubject(subject);

 msg.setContent(message, "text/html");

 File randomFile = File.createTempFile("rnd", "txt");
 randomFile.deleteOnExit();
 helper.addAttachment("rnd", randomFile);

 sender.send(msg);

Any help is appreciated


Solution

  • Set the message content using MimeMessage and not the MimeMessage.Change from msg.setContent(message,"text/html") to helper.setText(message,true).Using the MimeMessage setContent will set the content for the whole message