|
5 | 5 | import jakarta.mail.MessagingException;
|
6 | 6 | import jakarta.mail.internet.MimeMessage;
|
7 | 7 | import jakarta.mail.util.ByteArrayDataSource;
|
| 8 | +import java.io.BufferedInputStream; |
8 | 9 | import java.io.IOException;
|
9 | 10 | import java.io.InputStream;
|
10 | 11 | import java.util.Map;
|
11 | 12 | import lombok.RequiredArgsConstructor;
|
12 | 13 | import lombok.extern.slf4j.Slf4j;
|
| 14 | +import org.apache.tika.Tika; |
13 | 15 | import org.springframework.mail.javamail.JavaMailSender;
|
14 | 16 | import org.springframework.mail.javamail.MimeMessageHelper;
|
15 | 17 | import org.springframework.stereotype.Service;
|
|
20 | 22 | public class MailAdapter implements SendMailOutPort {
|
21 | 23 | private final JavaMailSender mailSender;
|
22 | 24 | private final MailProperties mailProperties;
|
| 25 | + private final Tika tika = new Tika(); |
23 | 26 |
|
24 | 27 | @Override
|
25 | 28 | public void sendMail(final MailMessage mailMessage) {
|
26 | 29 | final MimeMessage mimeMessage = mailSender.createMimeMessage();
|
27 | 30 | try {
|
28 |
| - final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf-8"); |
| 31 | + final MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, MimeMessageHelper.MULTIPART_MODE_MIXED); |
29 | 32 | helper.setFrom(mailProperties.getFromAddress());
|
30 | 33 | helper.setTo(mailMessage.getRecipient());
|
31 | 34 | helper.setSubject(mailMessage.getSubject());
|
32 | 35 | helper.setText(mailMessage.getBody(), true);
|
33 | 36 | for (final Map.Entry<String, InputStream> entry : mailMessage.getAttachments().entrySet()) {
|
34 | 37 | //TODO is there a way to do this with streaming? The following line loads the attachment into RAM...
|
35 |
| - final DataSource dataSource = new ByteArrayDataSource(entry.getValue(), "application/octet-stream"); |
36 |
| - helper.addAttachment(entry.getKey(), dataSource); |
37 |
| - //maybe like this? |
38 |
| - //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 | + } |
39 | 46 | }
|
40 | 47 | mailSender.send(mimeMessage);
|
41 | 48 | } catch (final MessagingException | IOException e) {
|
42 | 49 | throw new RuntimeException(e);
|
43 | 50 | }
|
44 | 51 | }
|
45 | 52 |
|
| 53 | + public String detectMimeType(final InputStream inputStream) throws IOException { |
| 54 | + return tika.detect(inputStream); |
| 55 | + } |
| 56 | + |
46 | 57 | // private class InputStreamSourceImpl implements InputStreamSource {
|
47 | 58 | // private final InputStream inputStream;
|
48 | 59 | //
|
|
0 commit comments