|
5 | 5 | import jakarta.mail.MessagingException;
|
6 | 6 | import jakarta.mail.internet.MimeMessage;
|
7 | 7 | import jakarta.mail.util.ByteArrayDataSource;
|
8 |
| - |
9 | 8 | import java.io.BufferedInputStream;
|
10 | 9 | import java.io.IOException;
|
11 | 10 | import java.io.InputStream;
|
@@ -36,21 +35,22 @@ public void sendMail(final MailMessage mailMessage) {
|
36 | 35 | helper.setText(mailMessage.getBody(), true);
|
37 | 36 | for (final Map.Entry<String, InputStream> entry : mailMessage.getAttachments().entrySet()) {
|
38 | 37 | //TODO is there a way to do this with streaming? The following line loads the attachment into RAM...
|
39 |
| - InputStream bis = new BufferedInputStream(entry.getValue()); |
40 |
| - String mime = detectMimeType(bis); |
41 |
| - log.debug("Mime-Type for " + entry.getKey() + " found " + mime); |
42 |
| - final DataSource dataSource = new ByteArrayDataSource(bis, mime); |
43 |
| - helper.addAttachment(entry.getKey(), dataSource); |
44 |
| - //maybe like this? |
45 |
| - //helper.addAttachment(e.getKey(), new InputStreamSourceImpl(e.getValue())); |
| 38 | + try (InputStream bis = new BufferedInputStream(entry.getValue())) { |
| 39 | + final String mime = detectMimeType(bis); |
| 40 | + log.debug("Mime-Type for " + entry.getKey() + " found " + mime); |
| 41 | + final DataSource dataSource = new ByteArrayDataSource(bis, mime); |
| 42 | + helper.addAttachment(entry.getKey(), dataSource); |
| 43 | + //maybe like this? |
| 44 | + //helper.addAttachment(e.getKey(), new InputStreamSourceImpl(e.getValue())); |
| 45 | + } |
46 | 46 | }
|
47 | 47 | mailSender.send(mimeMessage);
|
48 | 48 | } catch (final MessagingException | IOException e) {
|
49 | 49 | throw new RuntimeException(e);
|
50 | 50 | }
|
51 | 51 | }
|
52 | 52 |
|
53 |
| - public String detectMimeType(InputStream inputStream) throws IOException { |
| 53 | + public String detectMimeType(final InputStream inputStream) throws IOException { |
54 | 54 | return tika.detect(inputStream);
|
55 | 55 | }
|
56 | 56 |
|
|
0 commit comments