Skip to content

Commit 2420fa6

Browse files
Enhance email functionality by adding support for input stream attachments and custom headers
1 parent f9e2965 commit 2420fa6

3 files changed

Lines changed: 65 additions & 25 deletions

File tree

extensions/email-sms/sources/core/src/main/java/tools/dynamia/modules/email/EmailAttachment.java

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,46 @@
1717

1818
package tools.dynamia.modules.email;
1919

20+
import org.springframework.core.io.InputStreamSource;
21+
2022
import java.io.File;
2123

2224
/**
2325
* Represents an attachment in an email.
2426
*/
2527
public class EmailAttachment {
2628

27-
private String name;
28-
private File file;
29+
private final String name;
30+
private File file;
31+
32+
private InputStreamSource inputStreamSource;
33+
34+
public EmailAttachment(File file) {
35+
super();
36+
this.file = file;
37+
this.name = file.getName();
38+
}
2939

30-
public EmailAttachment(File file) {
31-
super();
32-
this.file = file;
33-
this.name = file.getName();
34-
}
40+
public EmailAttachment(String name, File file) {
41+
super();
42+
this.name = name;
43+
this.file = file;
44+
}
3545

36-
public EmailAttachment(String name, File file) {
37-
super();
38-
this.name = name;
39-
this.file = file;
40-
}
46+
public EmailAttachment(String name, InputStreamSource inputStreamSource) {
47+
this.name = name;
48+
this.inputStreamSource = inputStreamSource;
49+
}
4150

42-
public File getFile() {
43-
return file;
44-
}
51+
public File getFile() {
52+
return file;
53+
}
4554

46-
public String getName() {
47-
return name;
48-
}
55+
public String getName() {
56+
return name;
57+
}
4958

59+
public InputStreamSource getInputStreamSource() {
60+
return inputStreamSource;
61+
}
5062
}

extensions/email-sms/sources/core/src/main/java/tools/dynamia/modules/email/EmailMessage.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public static boolean isEmailAddress(String email) {
7373
private boolean notification;
7474
private String notificationUuid;
7575

76+
private Map<String, String> headers = new HashMap<>();
77+
7678
/**
7779
* Represents an email message that can be sent.
7880
*/
@@ -320,4 +322,16 @@ public Set<String> loadAllAddresses() {
320322
all.addAll(getBccs());
321323
return all;
322324
}
325+
326+
public void addHeader(String name, String value) {
327+
headers.put(name, value);
328+
}
329+
330+
public String getHeader(String name) {
331+
return headers.get(name);
332+
}
333+
334+
public Map<String, String> getHeaders() {
335+
return headers;
336+
}
323337
}

extensions/email-sms/sources/core/src/main/java/tools/dynamia/modules/email/services/impl/EmailServiceImpl.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
package tools.dynamia.modules.email.services.impl;
2020

21+
import jakarta.mail.MessagingException;
2122
import jakarta.mail.internet.MimeMessage;
2223
import org.springframework.mail.MailSender;
2324
import org.springframework.mail.javamail.JavaMailSenderImpl;
@@ -34,7 +35,6 @@
3435
import tools.dynamia.domain.query.QueryConditions;
3536
import tools.dynamia.domain.query.QueryParameters;
3637
import tools.dynamia.domain.services.CrudService;
37-
import tools.dynamia.domain.util.CrudServiceListenerAdapter;
3838
import tools.dynamia.integration.Containers;
3939
import tools.dynamia.integration.scheduling.SchedulerUtil;
4040
import tools.dynamia.integration.scheduling.TaskException;
@@ -62,7 +62,6 @@
6262
import java.util.Properties;
6363
import java.util.Set;
6464
import java.util.concurrent.CompletableFuture;
65-
import java.util.concurrent.Future;
6665

6766
/**
6867
* @author Mario Serrano Leones
@@ -191,7 +190,7 @@ private EmailSendResult processAndSendEmail(EmailMessage mailMessage, EmailAccou
191190
helper.setTo(mailMessage.getTo().split(","));
192191
} else {
193192
if (!mailMessage.getTos().isEmpty())
194-
helper.setTo(tosAsArray[0].toString());
193+
helper.setTo(tosAsArray[0]);
195194
}
196195

197196
if (!mailMessage.getTos().isEmpty()) {
@@ -222,8 +221,24 @@ private EmailSendResult processAndSendEmail(EmailMessage mailMessage, EmailAccou
222221
helper.setReplyTo(mailMessage.getReplyTo());
223222
}
224223

225-
for (EmailAttachment archivo : mailMessage.getAttachments()) {
226-
helper.addAttachment(archivo.getName(), archivo.getFile());
224+
if (mailMessage.getAttachments() != null) {
225+
for (EmailAttachment attachment : mailMessage.getAttachments()) {
226+
if (attachment.getFile() != null && attachment.getFile().exists()) {
227+
helper.addAttachment(attachment.getName(), attachment.getFile());
228+
} else if (attachment.getInputStreamSource() != null) {
229+
helper.addAttachment(attachment.getName(), attachment.getInputStreamSource());
230+
}
231+
}
232+
}
233+
234+
if (mailMessage.getHeaders() != null && !mailMessage.getHeaders().isEmpty()) {
235+
mailMessage.getHeaders().forEach((k, v) -> {
236+
try {
237+
mimeMessage.addHeader(k, v);
238+
} catch (MessagingException e) {
239+
logger.warn("Error adding header " + k + " to email message " + mailMessage, e);
240+
}
241+
});
227242
}
228243

229244
fireOnMailSending(mailMessage);
@@ -346,7 +361,7 @@ public EmailTemplate getTemplateByName(String name) {
346361
return getTemplateByName(name, true);
347362
}
348363

349-
private MailSender createMailSender(EmailAccount account) {
364+
protected MailSender createMailSender(EmailAccount account) {
350365
JavaMailSenderImpl mailSender = (JavaMailSenderImpl) MAIL_SENDERS.get(account.getId());
351366
if (mailSender == null) {
352367
logger.info("Creating Mail Sender for: " + account);
@@ -508,7 +523,6 @@ private void fireOnMailSendFail(EmailMessage message, Throwable cause) {
508523
}
509524

510525

511-
512526
@Override
513527
public void logEmailAddress(EmailAccount emailAccount, EmailMessage message) {
514528
try {

0 commit comments

Comments
 (0)