@@ -90,8 +90,6 @@ public void sendEmail(String fromName, String fromAddress, String subject, Strin
9090 return ;
9191 }
9292
93- List <JSONArray > emailBatches = getEmailBatches (recipients );
94- List <JSONArray > failedBatches = new ArrayList <>();
9593 JSONObject sender = new JSONObject ()
9694 .put ("Email" , fromAddress )
9795 .put ("Name" , fromName );
@@ -106,27 +104,33 @@ public void sendEmail(String fromName, String fromAddress, String subject, Strin
106104 final String localEmailFormat = modifiedEmailFormat ;
107105 final String localContent = content ;
108106
109- emailBatches .forEach ((recipientList ) -> {
110- MailjetRequest request = new MailjetRequest (Emailv31 .resource )
111- .property (Emailv31 .MESSAGES , new JSONArray ()
112- .put (new JSONObject ()
113- .put (Emailv31 .Message .FROM , sender )
114- .put (Emailv31 .Message .TO , new JSONArray ().put (sender ))
115- .put (Emailv31 .Message .BCC , recipientList )
116- .put (Emailv31 .Message .SUBJECT , subject )
117- .put (localEmailFormat , localContent )));
107+ if (recipients .length == 1 ) {
108+ final JSONArray to = new JSONArray ()
109+ .put (new JSONObject ().put ("Email" , recipients [0 ]));
118110 try {
119- MailjetResponse response = client .post (request );
120- LOG .info ("Mailjet response status: {}" , response .getStatus ());
121- LOG .info ("Mailjet response data: {}" , response .getData ());
111+ send (sender , to , null , subject , localEmailFormat , localContent );
122112 } catch (MailjetException e ) {
123- LOG .error ("An unexpected error occurred while sending the upload notification: {}" , e .getLocalizedMessage (), e );
124- failedBatches .add (recipientList );
113+ throw new BadArgException ("Failed to send email for " + to );
114+ }
115+ } else {
116+ List <JSONArray > emailBatches = getEmailBatches (recipients );
117+ List <JSONArray > failedBatches = new ArrayList <>();
118+ final JSONArray to =
119+ new JSONArray ()
120+ .put (new JSONObject ().put ("Email" , this .fromAddress ));
121+ emailBatches .forEach ((recipientList ) -> {
122+ try {
123+ send (sender , to , recipientList ,
124+ subject , localEmailFormat , localContent );
125+ } catch (MailjetException e ) {
126+ LOG .error ("An unexpected error occurred while sending the upload notification: {}" , e .getLocalizedMessage (), e );
127+ failedBatches .add (recipientList );
128+ }
129+ });
130+
131+ if (!failedBatches .isEmpty ()) {
132+ throw new BadArgException ("Failed to send emails for " + failedBatches );
125133 }
126- });
127-
128- if (!failedBatches .isEmpty ()) {
129- throw new BadArgException ("Failed to send emails for " + failedBatches );
130134 }
131135 }
132136
@@ -154,4 +158,22 @@ public void setEmailFormat(String format) {
154158 throw new BadArgException (String .format ("Email format must be either HTMLPART, MJMLPART or TEXTPART, got %s" , format ));
155159 }
156160 }
161+
162+ private void send (JSONObject from , JSONArray to , JSONArray bcc ,
163+ String subject , String emailFormat , String content ) throws MailjetException {
164+ JSONObject values = new JSONObject ()
165+ .put (Emailv31 .Message .FROM , from )
166+ .put (Emailv31 .Message .TO , to )
167+ .put (Emailv31 .Message .SUBJECT , subject )
168+ .put (emailFormat , content );
169+ if (bcc != null ) {
170+ values .put (Emailv31 .Message .BCC , bcc );
171+ }
172+
173+ MailjetRequest request = new MailjetRequest (Emailv31 .resource )
174+ .property (Emailv31 .MESSAGES , new JSONArray ().put (values ));
175+ MailjetResponse response = client .post (request );
176+ LOG .info ("Mailjet response status: {}" , response .getStatus ());
177+ LOG .info ("Mailjet response data: {}" , response .getData ());
178+ }
157179}
0 commit comments