Spring - String as an Email Attachment

Use the following code to attach a string(as file) with email.

String exampleString = "test";
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
MimeMessage message = sender.createMimeMessage();
                        MimeMessageHelper helper = new MimeMessageHelper(message, true);
                        helper.setTo("<toEmail>");
                        helper.setFrom("<fromEmailAddress>");
                        helper.addAttachment("test.txt",
                                        stream);

Search