1818import jakarta .mail .util .ByteArrayDataSource ;
1919import java .io .ByteArrayOutputStream ;
2020import java .io .IOException ;
21- import java .util .ArrayList ;
2221import java .util .List ;
2322import org .bouncycastle .openpgp .PGPPublicKey ;
2423import org .springframework .beans .factory .annotation .Autowired ;
@@ -62,25 +61,13 @@ public void sendEmail(
6261 String message ,
6362 List <DataAttachment > attachments )
6463 throws Exception {
65- MimeMessage mimeMessage =
66- buildMimeMessage (from , replyTos , inReplyTo , subject , message , attachments );
67- List <InternetAddress > recipients = new ArrayList <>();
68- for (ExecutionContext userContext : usersContext ) {
69- recipients .add (new InternetAddress (userContext .getUser ().getEmail ()));
70- }
71- mimeMessage .setRecipients (Message .RecipientType .TO , recipients .toArray (InternetAddress []::new ));
72- this .sendEmailWithRetry (execution , mimeMessage );
73- String emails = usersContext .stream ().map (c -> c .getUser ().getEmail ()).collect (joining (", " ));
74- List <String > userIds = usersContext .stream ().map (c -> c .getUser ().getId ()).toList ();
75- execution .addTrace (
76- getNewSuccessTrace ("Mail sent to " + emails , ExecutionTraceAction .EXECUTION , userIds ));
77- // Store message in Imap after sending
78- storeMessageImap (execution , mimeMessage , userIds );
64+ sendEmail (
65+ execution , usersContext , from , replyTos , inReplyTo , false , subject , message , attachments );
7966 }
8067
8168 public void sendEmail (
8269 Execution execution ,
83- ExecutionContext userContext ,
70+ List < ExecutionContext > usersContext ,
8471 String from ,
8572 List <String > replyTos ,
8673 String inReplyTo ,
@@ -89,24 +76,50 @@ public void sendEmail(
8976 String message ,
9077 List <DataAttachment > attachments )
9178 throws Exception {
92- String email = userContext .getUser ().getEmail ();
93- String contextualSubject = buildContextualContent (subject , userContext );
94- String contextualBody = buildContextualContent (message , userContext );
79+ ExecutionContext interpolationContext = (ExecutionContext ) usersContext .getFirst ().clone ();
80+ if (usersContext .size () > 1 ) {
81+ interpolationContext .remove ("user" );
82+ }
83+ String contextualSubject = buildContextualContent (subject , interpolationContext );
84+ String contextualBody = buildContextualContent (message , interpolationContext );
9585
9686 MimeMessage mimeMessage =
9787 buildMimeMessage (from , replyTos , inReplyTo , contextualSubject , contextualBody , attachments );
98- mimeMessage .setRecipient (Message .RecipientType .TO , new InternetAddress (email ));
99- // Crypt if needed
100- if (mustBeEncrypted ) {
88+ mimeMessage .setRecipients (
89+ Message .RecipientType .TO ,
90+ usersContext .stream ()
91+ .map (
92+ uc -> {
93+ try {
94+ return new InternetAddress (uc .getUser ().getEmail ());
95+ } catch (AddressException e ) {
96+ throw new RuntimeException (e );
97+ }
98+ })
99+ .toArray (InternetAddress []::new ));
100+
101+ // request encryption but this is possible only for an email to a single recipient
102+ if (mustBeEncrypted && usersContext .size () == 1 ) {
103+ ExecutionContext singleUserContext = usersContext .getFirst ();
101104 MimeMessage encMessage =
102- getEncryptedMimeMessage (userContext , from , replyTos , subject , email , mimeMessage );
105+ getEncryptedMimeMessage (
106+ singleUserContext ,
107+ from ,
108+ replyTos ,
109+ subject ,
110+ singleUserContext .getUser ().getEmail (),
111+ mimeMessage );
103112 this .sendEmailWithRetry (execution , encMessage );
104113 } else {
105114 this .sendEmailWithRetry (execution , mimeMessage );
106115 }
107- List <String > userIds = List . of ( userContext . getUser ().getId ());
116+ List <String > userIds = usersContext . stream (). map ( c -> c . getUser ().getId ()). toList ( );
108117 execution .addTrace (
109- getNewSuccessTrace ("Mail sent to " + email , ExecutionTraceAction .EXECUTION , userIds ));
118+ getNewSuccessTrace (
119+ "Mail sent to "
120+ + usersContext .stream ().map (c -> c .getUser ().getEmail ()).collect (joining (", " )),
121+ ExecutionTraceAction .EXECUTION ,
122+ userIds ));
110123 // Store message in Imap after sending
111124 storeMessageImap (execution , mimeMessage , userIds );
112125 }
0 commit comments